Append to File in C++

Appending Data to a File in C++.

BeginnerFile Handling ProgramsExample 3 of 6
append-to-file.cpp
1#include <iostream>
2#include <fstream>
3using namespace std;
4
5int main() {
6 // Open file in append mode
7 ofstream outFile("data.txt", ios::app);
8
9 if (outFile.is_open()) {
10 // Append data to existing file
11 outFile << "\nAppended line 1" << endl;
12 outFile << "Appended line 2" << endl;
13 outFile << "Appended line 3" << endl;
14
15 outFile.close();
16 cout << "Data appended to file successfully!" << endl;
17 } else {
18 cout << "Error: Unable to open file for appending." << endl;
19 }
20
21 // Read and display updated file
22 ifstream inFile("data.txt");
23 if (inFile.is_open()) {
24 string line;
25 cout << "\nUpdated file contents:" << endl;
26 while (getline(inFile, line)) {
27 cout << line << endl;
28 }
29 inFile.close();
30 }
31
32 return 0;
33}

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

What's going on

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.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <fstream>
Provides file stream classes (ofstream, ifstream).

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

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()) {

}

1// File opened successfully

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

Verifying Appended Content

Reading File

:

ifstream inFile("data.txt");

string line;

while (getline(inFile, line)) {

}

1cout << line << endl;

How it works

:

Read entire file
Shows original + appended content
Verifies append operation
Confirms data preservation

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

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

return 0;

This ends the program successfully.