#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Open file in append mode
ofstream outFile("data.txt", ios::app);
if (outFile.is_open()) {
// Append data to existing file
outFile << "\nAppended line 1" << endl;
outFile << "Appended line 2" << endl;
outFile << "Appended line 3" << endl;
outFile.close();
cout << "Data appended to file successfully!" << endl;
} else {
cout << "Error: Unable to open file for appending." << endl;
}
// Read and display updated file
ifstream inFile("data.txt");
if (inFile.is_open()) {
string line;
cout << "\nUpdated file contents:" << endl;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
}
return 0;
}Output
Data appended to file successfully! Updated file contents: Hello, World! This is a C++ file handling program. Line 3: Learning file operations. 100 200 300 Appended line 1 Appended line 2 Appended line 3
This program teaches you how to Append to a File in C++. Appending adds data to the end of an existing file without overwriting the original content. This is essential for logging, adding records, and preserving existing data.
1. What This Program Does
The program demonstrates file appending operations:
- Opening file in append mode using ios::app
- Adding data to end of existing file
- Preserving original file content
- Verifying appended content
File appending enables adding data without losing existing content.
2. Header Files Used
-
#include <iostream>
- Provides cout and cin for input/output operations.
-
#include <fstream>
- Provides file stream classes (ofstream, ifstream).
3. Understanding File Appending
Append Mode Concept:
- Adds data to end of file
- Preserves existing content
- File pointer starts at end
- Creates file if doesn't exist
File Modes:
- ios::app: append mode (adds to end)
- ios::out: write mode (overwrites, default)
- ios::in: read mode
- Can combine: ios::app | ios::out
4. Opening File in Append Mode
Using ios::app:
ofstream outFile("data.txt", ios::app);
How it works:
- Opens file in append mode
- File pointer at end of file
- New data added after existing
- Creates file if doesn't exist
Checking Success:
if (outFile.is_open()) { // File opened successfully }
5. Appending Data
Writing in Append Mode:
outFile << "\nAppended line 1" << endl; outFile << "Appended line 2" << endl;
How it works:
- Data written at end of file
- Existing content preserved
- Newlines separate additions
- Sequential appending
6. Verifying Appended Content
Reading File:
ifstream inFile("data.txt"); string line; while (getline(inFile, line)) { cout << line << endl; }
How it works:
- Read entire file
- Shows original + appended content
- Verifies append operation
- Confirms data preservation
7. When to Use Append Mode
Best For:
- Logging events
- Adding records
- Preserving existing data
- Building files incrementally
- Appending to logs
Example Scenarios:
- Event logging
- Adding user data
- Building reports
- Appending to logs
- Incremental data collection
8. Important Considerations
Append vs Write:
- Append: adds to end (preserves data)
- Write: overwrites (loses data)
- Choose based on requirement
- Be careful with mode selection
File Position:
- Append mode: pointer at end
- Write mode: pointer at beginning
- New data always added at end in append
- Cannot insert in middle
Newlines:
- Add newlines between appends
- Use endl or "\n"
- Maintains file formatting
- Prevents data concatenation
9. return 0;
This ends the program successfully.
Summary
- File appending: uses ios::app mode flag with ofstream to add data to end of file.
- Preserves existing content, adds new data after original content.
- File pointer starts at end, new data appended sequentially.
- Creates file if doesn't exist, preserves content if exists.
- Understanding file appending enables incremental data addition.
- Essential for logging, adding records, and preserving existing files.
This program is fundamental for learning file modes, understanding data preservation, and preparing for logging and incremental file operations in C++ programs.