Home »
C++ programs »
C++ file handling programs
How to append text to a text file in C++?
In this tutorial, we will learn how can we append text to a text file in C++?
By IncludeHelp Last updated : October 30, 2023
Problem statement
Given a text file, write a C++ program to append text in it.
Appending text to a text file in C++
- To open the file in append mode, use the ofstream class.
- Use the ios::app flag in the open() function to specify the append mode of the file to ensure that new data is appended to the existing file content.
- And, use the is_open() function to check whether the file is successfully opened or not.
- If the file is open, then input data from the user to append to the file.
- And then, use the output stream operator (<<) to append the input data to the file.
- After that, close the file using the close() function.
C++ program to append text to a text file
In this example, we have a file named "file1.txt" with some text, we are taking input from the user and appending text to this file. Here, we have created a user-defined function readAndPrintFile() to print the content of the given file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Function to read and print the file
void readAndPrintFile(string fname) {
ifstream file(fname);
string data;
// check whether file is opened or not
if (file.is_open()) {
cout << "File's data is:" << endl;
// read file's data
while (getline(file, data)) {
cout << data << endl;
}
// close the file
file.close();
} else {
cout << "There is some error to open the file." << endl;
}
}
// Main function
int main() {
// print the existing data of the file
readAndPrintFile("file1.txt");
cout << endl;
ofstream fileObj;
// Open the file in append mode
fileObj.open("file1.txt", ios::app);
// check file is opened or not
if (fileObj.is_open()) {
string newData;
// input data from the user to append in the file
cout << "Enter new data to append in the file: ";
getline(cin, newData);
// append data
fileObj << newData << endl;
// close the file
fileObj.close();
cout << "Data appended successfully." << endl;
cout << endl;
// Again, print the data of the updated file
readAndPrintFile("file1.txt");
cout << endl;
} else {
cout << "There is some error to open the file." << endl;
}
return 0;
}
File (file1.txt)
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
Output
File's data is:
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
Enter new data to append in the file: C++ is a high-level, general-purpose programming language created by Danish computer scientist Bjarne Stroustrup.
Data appended successfully.
File's data is:
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
C++ is a high-level, general-purpose programming language created by Danish computer scientist Bjarne Stroustrup.