DEV Community

Cover image for Write to a file in c++
CoderLegion
CoderLegion

Posted on • Updated on • Originally published at kodblems.com

Write to a file in c++

The C programming language is the foundation of many languages, so knowing it will make it easier for you to get to grips with many similar environments. But, in addition to knowing what this language involves, it is essential to delve into its history, even if it happened almost 50 years ago.

Well, localizing yourself over time will not only give you a better understanding of this very important language in the programming world, but it will also help your training. In addition, we will also assess how and why it was born, and what this language actually means C.

It all started in 1970, in the laboratory of the Bell company, when Dennis Ritchie and Brian Kernighan started creating C. Ritchie was a collaborator of Ken Thompson, a computer scientist who now worked on UNIX (operating system) and who had also created the B language.

By the end you will know how to write lines to a plain text file. We will see two modes, the output mode for writing replacing the content, and the append mode for writing adding the content. All this programming in C ++.

Opening the file
We must declare a type variable ofstream, which will point to the file. Then we invoke the method open, passing it the name of the file and the opening mode . I'll show you two:

output : it is simply written over the file, replacing the content that exists in it. It is in fstream::out.
append : adds the content to the end of the file, so it does not replace the existing one. It is in fstream::app.
For the example I will use fstream::out.

string filename = "videogames.txt";
ofstream file;
// We open the file
file.open (filename.c_str (), fstream :: out);
Writing content
We are going to write content in the file simply redirecting to it using <<as well as when we print in the console redirecting to cout; well, that's how it's done in C ++

By the way, at the end of writing, we close the file with close

// And we write redirecting
file << "cuph";
file << "ead";
// Let's not forget line breaks
file << endl;
// We can write more
file << "doom eternal" << endl;
file << "doom 2016" << endl;
file << "resident evil 2" << endl;
// Finally we close it
file.close ();
cout << "Written correctly";
Putting it all together
The complete code to save content to a plain text file using C ++ is as follows:

include

include
using namespace std;
/ *
sitename
/

int main () {
string filename = "videogames.txt";
ofstream file;
// We open the file
file.open (filename.c_str (), fstream :: out);
// And we write redirecting
file << "cuph";
file << "ead";
// Let's not forget line breaks
file << endl;
// We can write more
file << "doom eternal" << endl;
file << "doom 2016" << endl;
file << "resident evil 2" << endl;
// Finally we close it
file.close ();
cout << "Written correctly";
}
Hope it helps.

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Also, you mis-tagged this post as #c. It should be #cpp.

Collapse
 
pauljlucas profile image
Paul J. Lucas

Please use Markdown properly and enclose code blocks in triple backticks to make it easier to read with proper indenting.