-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_append_data.cpp
42 lines (31 loc) · 959 Bytes
/
03_append_data.cpp
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
35
36
37
38
39
40
41
42
// // Write a C++ program to open a file 'output.txt' and append data to it.
// // Header files
#include <iostream>
#include <fstream>
// // use namespace
using namespace std;
int main()
{
// // specify file name
const char *fileName = "output.txt";
// // data to be appended
const char *appendData = "Cpp Programming";
// // create an instance of ofstream for writing in a file
ofstream fout;
// // open file for writing, if file not exist it will be created
fout.open(fileName, ios::app);
// // check if the file is successfully opened
if (!fout.is_open())
{
cout << "\nError: Unable to Open File..." << endl;
return 1;
}
// // append data in file using insertion operator
fout << appendData;
// // close file
fout.close();
// // data appended successfully
cout << "\nData Appended Successfully..." << endl;
cout << endl; // Add new line
return 0;
}