DEV Community

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

Posted on β€’ Edited on β€’ Originally published at kodblems.com

2 1

Write to a file in c++

πŸŽ‰ Before you dive into this article...

πŸš€ Check out our vibrant new community at CoderLegion.com!

πŸ’‘ Share your knowledge, connect with like-minded developers, and grow together.

πŸ‘‰ Click here to join now!

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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

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.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

πŸ‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Communityβ€”every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple β€œthank you” goes a long wayβ€”express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay