-
Notifications
You must be signed in to change notification settings - Fork 1
/
b2oops.cpp
43 lines (43 loc) · 1008 Bytes
/
b2oops.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
43
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
int main (){
char name[80];
cout<<"Enter name of file to create\n";
cin>>name;
// open a file in write mode.
ofstream fout;
fout.open(name);
if(!fout){
cout<<"Error opening file\n";
exit(1);
}
cout << "Writing to the file" << endl;
cout << "Enter contents for file end with ctrl+D";
string data;
while(getline(cin,data)){
if(data == "^D")
break;
// write inputted data into the file.
fout << data << endl;
}
// close the opened file.
fout.close();
// open a file in read mode.
ifstream fin;
fin.open(name);
if(!fin){
cout<<"Error opening file\n";
exit(1);
}
cout << "Reading from the file" << endl;
while(fin){
getline(fin,data);
// write the data at the screen.
cout << data << endl;
}
// close the opened file.
fin.close();
return 0;
}