-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathfile_processing.cpp
More file actions
53 lines (44 loc) · 1.14 KB
/
Copy pathfile_processing.cpp
File metadata and controls
53 lines (44 loc) · 1.14 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
46
47
48
49
50
51
52
53
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
void writeData(){
string enrollment;
string name;
string phone;
int age;
ofstream fileOut("students.txt", ios::out);
//ofstream fileOut("students.txt", ios::app);
//ofstream fileOut("students.txt");
if(!fileOut){
cerr << "File couldn't openend" << endl;
exit(1);
}
cout<<"Enter the Enrollment, Name, Phone No and Age"<<endl;
cout<<"Enter end-of-file (EOF) to end the input"<<endl;
while(cin>>enrollment>>name>>phone>>age){
fileOut<<enrollment<<" "<<name<<" "<<phone<<" "<<age<<endl;
}
fileOut.close();
}
void readData(){
string enrollment;
string name;
string phone;
int age;
//Principle of the least privilege
ifstream fileIn("students.txt", ios::in);
if(!fileIn){
cerr << "File couldn't openend" << endl;
exit(1);
}
cout<<left<<setw(15)<<"Enrollment"<<setw(10)<<"Name"<<setw(12)<<"Phone No"<<setw(3)<<"Age"<<endl;
while(fileIn>>enrollment>>name>>phone>>age){
cout<<left<<setw(15)<<enrollment<<setw(10)<<name<<setw(12)<<phone<<setw(3)<<age<<endl;
}
}
int main(){
writeData();
readData();
}