-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
30 lines (26 loc) · 921 Bytes
/
Copy pathfile.cpp
File metadata and controls
30 lines (26 loc) · 921 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
#include <fstream>
#include <iostream>
using namespace std;
int main () {
char data[100];
ofstream outfile; // open a file in write mode.
outfile.open("afile.dat");
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
outfile << data << endl; // write inputted data into the file.
cout << "Enter your age: ";
cin >> data;
cin.ignore();
outfile << data << endl; //write inputted data into the file.
outfile.close(); // close the opened file.
ifstream infile; // open a file in read mode.
infile.open("afile.dat");
cout << "Reading from the file" << endl;
infile >> data;
cout << data << endl;
infile >> data; // again read the data from the file and display it.
cout << data << endl;
infile.close(); // close the opened file.
return 0;
}