How to Write Text Files with C++
Last Update: Jun 7, 2024
I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.
If you’re reading this tutorial, it’s likely because you googled how to read and write text files with C++. And it’s probably a pretty lucky guess you’re a beginner, so I’ll try to explain as much as I can here to make it clear.
Note: I wrote this article over ten years ago, and I’m re-writing it. Let me know what you think it should include.
If you’re thinking of getting into C++, hopefully this example will show you that it’s not as complicated as commonly thought. C++ can be tough, but the performance advantages are worth the effort. Java and C# are stealing market share from C++, but it still has its place in the software development world, mainly in applications that require high performance. But that’s for another article. Let’s get started.
For this example, I’m using Bloodshed Dev C++, because it’s simple, fast, and free. You could just as easily use Visual C++ express, also free, but I feel that Dev C++ is far simpler and better for learning the code.
Go to file->new and create a new project. Your options should look like this:
Start a new C++ project, and call it whatever you want. You can save the dev file wherever you feel necessary. Go into options and choose a folder to put your executable:
Select Project->Project Options->(Build Options)
This will put your final executable in a folder you can quickly get to when you decide to run it.
When you first create your project, you’ll find the following code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}
This code is pretty self-explanatory, and I won’t go into it here because we’re going to erase it anyway. Select all the code in the window and delete it. Replace it with this:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("test.txt");
myfile << "Hey this is my first written text file.\n";
myfile.close();
return 0;
}
After you have entered this code, save the file, then press Ctrl+F9. This will compile your executable. Run it from the command prompt, wherever you set it to build to. In my case, it looks like this:
When you run the executable, it generates test.txt. Everything you specified in your cout gets written to a file. But let’s examine how that happened.
The following code is the start of your program:
int main () {
This is the starting point of your program. Wherever you see main() that is the method you use to start your program. By putting this in, you are telling the compiler where to begin. “Int” is the return type, which I will talk about in future tutorials. “int” stands for integer, which is what your main function must return. For this example, you don’t need to be concerned with that part, but in the future, you will create functions that return a datatype, and it will become more important.
The following opens up an output file stream:
ofstream myfile;
What this does is open up a stream for file output. You can get more info about ofstream here. By opening up this stream, you are preparing the compiler to output data to a file. You create an object called “myfile” that you can use to perform the necessary file output functions.
myfile.open ("test.txt");
As you’ve probably guessed, this is how you open your file. You are using that same ofstream object to open a file you specify within the parentheses. In our case, we are instructing the myfile object to open test.txt.
myfile << "Hey this is my first written text file.\n";
This portion of the code does the actual writing. You are essentially “pushing” the data to the file object using the « operator. You can see above the line of text and then followed by \n. At the end of the string, we put \n to indicate a new line. This is known as an “escape character” and is used to create a new line. Play around with this line and see what you can do with it. You can use \n as many times as needed to format your output with the proper line breaks.
Finally, we need to close the file:
myfile.close();
return 0;
By calling the close method of myfile, we are closing the file we wrote to. While not required, you should always close your files on execution. If you are building an application, whether it’s just one person using it or hundreds, you should always close the files for resource usage purposes. Too many files loaded into memory can be a bad thing. Note that we also return 0 as a “success flag” for the compiler. The program exits at this point.
When you run the executable, it will generate a text file, as you’ve specified. You can use these same methods to read another file, filter it, and build another file. You can also create something that will filter the strings being parsed, and write strings with different patterns, or pretty much anything you can imagine.
So copy this code, and try it out. If you have any questions or need general help, be sure to ask me. I will gladly help others trying to learn who are stuck on something. Till then, enjoy!!
If you’d like to learn more, here’s a great C++ fundamentals course.
Advanced C++ developers wanting to level up should check out High-performance computing in C++