How to Append Text to a File in C++

  1. Use std::ofstream and open() Method to Append Text to a File
  2. Use std::fstream and open() Method to Append Text to a File
  3. Use write() Method With std::fstream and open() to Append Text to a File

This article introduces multiple C++ methods to append text to a file.

Use std::ofstream and open() Method to Append Text to a File

At first, we should create an ofstream object and then call its member function open. This method takes a filename as a string object for the first argument. As a second argument, we can pass open mode by specifying predefined constant values shown in the following table:

Constant Explanation
std::ios_base::app seek to the end of stream before each writing
std::ios_base::binary open in binary mode
std::ios_base::in open for reading
std::ios_base::out open for writing
std::ios_base::trunc discard the contents of the stream when opening
std::ios_base::ate seek to the end of stream immediately after open

Notice that the following code sample creates a file named tmp.txt in the current working directory. You can pass a full path of a file to create or append in a different location.

#include <iostream>
#include <fstream>

using std::cout; using std::ofstream;
using std::endl; using std::string;

int main()
{
    string filename("tmp.txt");
    ofstream file_out;

    file_out.open(filename, std::ios_base::app);
    file_out << "Some random text to append." << endl;
    cout << "Done !" << endl;

    return EXIT_SUCCESS;
}

Output:

Done !

Use std::fstream and open() Method to Append Text to a File

Alternatively, we can implement the previous example using the fstream which provides an option to open a given file for reading and writing/appending. This method is a universal way to manipulate file streams. Notice that we also added validation if the statement with is_open call, which verifies if the stream is associated with the specified file.

#include <iostream>
#include <fstream>

using std::cout; using std::fstream;
using std::endl; using std::string;

int main()
{
    string filename("tmp.txt");
    fstream file;

    file.open(filename, std::ios_base::app | std::ios_base::in);
    if (file.is_open())
        file << "Some random text to append." << endl;
    cout << "Done !" << endl;

    return EXIT_SUCCESS;
}

Use write() Method With std::fstream and open() to Append Text to a File

Another method to solve this problem is to call the write member function explicitly from the fstream object rather than use the << operator as we’ve done in the previous examples. This method should be faster for most scenarios, but always profile your code and compare timings if you have strict performance requirements.

#include <iostream>
#include <fstream>

using std::cout; using std::fstream;
using std::endl; using std::string;

int main()
{
    string text("Some huge text to be appended");
    string filename("tmp.txt");
    fstream file;

    file.open(filename, std::ios_base::app);
    if (file.is_open())
        file.write(text.data(), text.size());
    cout << "Done !" << endl;

    return EXIT_SUCCESS;
}

Related Article - C++ File

  • How to Check if a File Exists in C++
  • How to Read a File Line by Line in C++
  • comments powered by Disqus