-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppendToFile.cpp
More file actions
34 lines (25 loc) · 972 Bytes
/
AppendToFile.cpp
File metadata and controls
34 lines (25 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*Write a program in C++ to open an existing file and
insert the text "My C++ file" at the end of it.
Incorporate suitable comments, to improve the code readability.*/
#include <iostream>
#include <fstream> // For file input/output operations
using namespace std;
int main() {
// File name and path of the existing file
string filename = "existing_file.txt";
// Open the file in append mode (ios::app) to add content at the end
ofstream outFile(filename, ios::app);
// Check if the file was opened successfully
if (!outFile) {
cout << "Error opening the file: " << filename << endl;
return 1;
}
// Text to be inserted at the end of the file
string newText = "My C++ file";
// Write the new text to the file
outFile << newText;
// Close the file after writing
outFile.close();
cout << "Text has been added to the file successfully." << endl;
return 0;
}