C++ Program to Append a String in an Existing File

Last Updated : 2 Sep, 2026

Appending to a file means adding new data at the end of an existing file without overwriting its current contents. In C++, file appending can be performed using the <fstream> library by opening a file in append mode (ios::app).

  • ios::app ensures that all new data is written at the end of the file.
  • Both ofstream and fstream can be used to append data to an existing file.

Example:

Suppose Geeks for Geeks.txt initially contains: -> Geeks for Geeks
After appending the string " String", the file becomes: -> Geeks for Geeks String

Approaches to Append a String to a File

The following approaches can be used to append a string to an existing file:

1. Using ofstream

The ofstream class is used for writing data to files. Opening a file with ios::app allows new data to be added without removing its existing contents.

Steps

  • Open the file using ofstream with ios::app.
  • Write the string to the file.
  • Close the file.
  • Open the file in read mode to display its updated contents.
C++
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    ofstream of;
    fstream f;

    // Open file in append mode
    of.open("Geeks for Geeks.txt", ios::app);

    if (!of)
        cout << "No such file found";
    else {
        of << " String";

        cout << "Data appended successfully\n";

        of.close();

        string word;

        // Read and display updated file
        f.open("Geeks for Geeks.txt");

        while (f >> word)
            cout << word << " ";

        f.close();
    }

    return 0;
}

Output
Data appended successfully
String 

Explanation

  • ofstream opens the file in append mode using ios::app.
  • The string " String" is added to the end of the file.
  • The file is then reopened using fstream to display the updated contents.

2. Using fstream

The fstream class supports both reading and writing. It can append data by opening the file with ios::in | ios::out | ios::app.

Steps

  • Open the file using fstream with append mode.
  • Write the string to the file.
  • Close the file.
  • Reopen the file in read mode and display its contents.
C++
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    fstream f;

    // Open file in append mode
    f.open("Geeks for Geeks.txt", ios::app);

    if (!f)
        cout << "No such file found";
    else {
        f << " String_fstream";

        cout << "Data appended successfully\n";

        f.close();

        string word;

        // Read and display updated file
        f.open("Geeks for Geeks.txt");

        while (f >> word)
            cout << word << " ";

        f.close();
    }

    return 0;
}

Output
Data appended successfully
String_fstream 

Explanation

  • fstream opens the file using the ios::app flag.
  • The string " String_fstream" is appended to the existing file contents.
  • The updated contents of the file are then displayed.

Difference Between ofstream and fstream

Featureofstreamfstream
PurposeOutput operations onlyInput and output operations
Supports append modeYesYes
Supports readingNoYes
Typical use caseWriting/appending dataReading and writing the same file

File Opening Mode Used

ModePurpose
ios::appOpens the file in append mode and writes new data at the end.
ios::inOpens the file for reading.
ios::outOpens the file for writing.
ios::in | ios::out | ios::appOpens the file for both reading and writing, with writing performed at the end.
Comment