Files
ooplab/lab10/30.cpp
Amneesh Singh 9e7cffdc32 lab 10: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-26 18:38:55 +05:30

68 lines
1.1 KiB
C++

#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;
}