How to Append Contents to a File in C++


C++


In this article, we show how to append contents to a file in C++.

Appending contents to a file will add contents to the file. If the file is blank, the contents you add will be added. If the file already has content, this content already in the file will remain and the new content that you append will be added to it.

If you simply use the write function without appending, any contents in the file will be overwritten.

But if your desired effect is to simply add contents to a file without erasing what's already in it, then you want to append to the file.

So let's say that you have an existing file with contents in it.

How can we add new lines, sentences, and paragraphs to this file?

Below we show how to do so in the program below.



We use the library to work with files. In this case, we are writing contents to a file.

We use the library to output an error message if the file, file1.txt, cannot be opened.

We use the library to create a string, which will be the contents of our file that we append to.

We create a variable, contents, which contains the line, "\nThis is a new line"

Since we add more than one line, we use the append() function to add a second line to our variable, contents. This skips 2 lines and adds, "This is a new paragraph"

Realize that this append() function has nothing to do with appending to our file itself. It is simply used in this case to append an additional line to our variable, so that we can write multiple lines to our file.

In order for us to append to a file, we must put the ofstream() function in append mode, which we explain in the next paragraph.

We then create our writer object. We use ofstream, instead of ifstream, because we are writing to the object, not reading from it. In this case, we are appending to the file. The important thing about the ofstream is not if you only specify the filename, it will write to the file. In order for us to append to the file, we add a second parameter. To append, this parameter is, ios::app

With the full line, ofstream writer("file1.txt", ios::app);, we now create a connection to open up the file1.txt file in order to append contents to the file. Without the second parameter, it would write to the file, which means it would overwrite any content in the file. We do not want to do this, so we add the second parameter to allow for appending.

We use an if statement to check if the writer object can be successfully opened. If not, we output an error message.

We then use the writer object and append to the file what is in the variable, contents. This is done through the line, writer << contents << endl;

After writing to the file, the writer object must be closed. This is done by the line, writer.close();

After running this program, you will see the new contents appended to the file. In this case, "This is a new line... This is a new paragraph"

And this is how to append contents to a file in C++.


Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...