67
lab10/30.cpp
Normal file
67
lab10/30.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
using namespace std;
|
||||
const string FILE_PATH = "./30.out";
|
||||
|
||||
class StudentInfo {
|
||||
char name[30];
|
||||
uint age;
|
||||
char sex;
|
||||
uint height;
|
||||
public:
|
||||
void read();
|
||||
void write();
|
||||
};
|
||||
|
||||
void StudentInfo::read() {
|
||||
cout << "Name: ";
|
||||
cin >> name;
|
||||
cout << "Age: ";
|
||||
cin >> age;
|
||||
cout << "Sex (m/f): ";
|
||||
cin >> sex;
|
||||
cout << "Height: ";
|
||||
cin >> height;
|
||||
cout << "[LOG] Data read\n";
|
||||
}
|
||||
|
||||
void StudentInfo::write() {
|
||||
ofstream file(FILE_PATH);
|
||||
file.close();
|
||||
file.open(FILE_PATH, ios_base::app);
|
||||
|
||||
file << "Name: " << name << endl
|
||||
<< "Age: " << age << endl
|
||||
<< "Sex: " << (sex == 'm' ? "Male" : "Female") << endl
|
||||
<< "Height: " << height << "cm";
|
||||
|
||||
cout << "[LOG] Data written\n";
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
int main() {
|
||||
StudentInfo foo;
|
||||
string line;
|
||||
|
||||
foo.read();
|
||||
foo.write();
|
||||
|
||||
ifstream file(FILE_PATH);
|
||||
|
||||
if (!file.is_open())
|
||||
exit(1);
|
||||
|
||||
cout << "File contents: \n";
|
||||
|
||||
while (getline(file, line)) {
|
||||
cout << line;
|
||||
if (file.peek() != EOF) {
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user