-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_fileio.cpp
More file actions
45 lines (37 loc) · 1 KB
/
09_fileio.cpp
File metadata and controls
45 lines (37 loc) · 1 KB
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
43
44
45
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
//create an output stream to write to the file
//append the new lines to the end of the file
ofstream myfileI ("input.txt", ios::app);
if (myfileI.is_open())
{
myfileI << "\nI am adding a line.\n";
myfileI << "I am adding another line.\n";
myfileI.close();
}
else cout << "Unable to open file for writing";
//create an input stream to read the file
ifstream myfileO ("input.txt");
//During the creation of ifstream, the file is opened.
//So we do not have explicitly open the file.
if (myfileO.is_open())
{
while ( getline (myfileO,line) )
{
cout << line << '\n';
}
myfileO.close();
}
else cout << "Unable to open file for reading";
return 0;
}
/*
The contents of input.txt as below,
Read and write to this file.
What am I doing here?
This is not a good example of a file
*/