
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to append text to a text file in C++?
Appending text means adding new content to an existing file without removing its current content. In C++, this can be done by opening the file in append mode and writing the specified content to it.
Steps to Append Text to a Text File
You need to follow the below steps to open a file in append mode and append the content to it:
- First of all, you need to include the <fstream> header file.
- Them, create an ofstream object to write to the file.
- Open the file in append mode using the ios::app flag.
- Use the << operator to append text to the file, text can be input through the user, store it into a variable and then use the variable instead of text to append in the file.
- Close the file using the file.close() function.
Algorithm
Following is the algorithm to append Text to a Text File in C++:
Begin Open that file data.txt as output file stream class object to perform output operation in append mode using fout file reference. If the file exists then Appending text to that file. Close fout. Open the file "data.txt" for reading the content of the file. Extracting the text from file and printing the text. End.
Example 1: Basic Program to Append Text to a File
This program opens a file in append mode and adds a new line of text to the end without deleting existing content. If the file doesn't exist, it creates one (the text):
#include<iostream> #include<fstream> // For file handling using namespace std; int main() { ofstream file; // Open file in append mode file.open("data.txt", ios::app); if (!file) { cout << "Error opening file!" << endl; return 1; } // Append text to the file file << "This is a new line added to the file.\n"; cout << "Text appended successfully!" << endl; // Close the file file.close(); return 0; }
Assume these as the text files(data.txt) as:
Hello World C++ File Handling
Output
If the "data.txt" file exists with text the output will be:
Hello World C++ File Handling This is a new line added to the file.
If it doesn't exists, the output will be
This is a new line added to the file.
The console output will be generated as:
Text appended successfully!
Example 2: Taking Input from User and Appending to File
This program takes a line of text from the user and appends it to a file named data.txt. If the file exists, the ,new text is added at the end and if the file doesn't exists it is automatically created and the text itself is written into it:
#include<iostream> #include<fstream> using namespace std; int main() { ofstream file; string userInput; // Open in append mode file.open("data.txt", ios::app); if (!file) { cout<<"Cannot open file!"<<endl; return 1; } cout<<"Enter text to append to the file: "; // Get full line including spaces getline(cin, userInput); file<<userInput<<endl; // Append user input cout<<"Text added to file successfully!"<<endl; // Close file file.close(); return 0; }
Output
Following is the output is as follows:
Enter text to append to the file: C++ is very interesting!
If data.txt exists, the output is :
Hello World C++ File Handling C++ is very interesting!
If data.txt doesnot exist, the output is:
C++ is very interesting!
The console output will be generated as:
Enter text to append to the file: C++ is very interesting! Text added to file successfully!