-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadAndWriteInFile.cpp
More file actions
47 lines (38 loc) · 895 Bytes
/
ReadAndWriteInFile.cpp
File metadata and controls
47 lines (38 loc) · 895 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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fileName = "ReadAndWriteFile";
string text;
ofstream outFile(fileName);
if (!outFile)
{
cout << "Error opening file for writing!" << endl;
}
cout << "Enter text to write to the file (type 'STOP' to end) : " << endl;
while (true)
{
getline(cin, text);
if (text == "STOP")
{
break;
}
outFile << text << endl;
}
outFile.close();
cout << "Text written to " << fileName << " Successfully." << endl;
ifstream inFile(fileName);
if (!inFile)
{
cerr << "Error opening file for reading!" << endl;
}
cout << "Content of " << fileName << " : " << endl;
while (getline(inFile, text))
{
cout << text << endl;
}
inFile.close();
return 0;
}