-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
51 lines (41 loc) · 1.21 KB
/
main.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
44
45
46
47
48
49
50
51
#include "student.hpp"
#include <fstream>
#include <ios>
#include <iostream>
#include <string>
int main(int argc, char **argv) {
if (argc < 3) {
std::cerr << "usage: " << argv[0] << " <name> <age>\n";
return 0x0;
}
std::ofstream *ofile = new std::ofstream();
std::ifstream *ifile = new std::ifstream();
// open file with truncate mode and binary mode
ofile->open("student.dat", std::ios::binary | std::ios::trunc);
// setup student object
Student *student =
new Student(argv[1], static_cast<unsigned short>(std::stoul(argv[2])));
// write binary content into student
ofile->write(reinterpret_cast<char *>(student), sizeof(*student));
ofile->close();
ifile->open("student.dat", std::ios::binary);
if (ifile->is_open()) {
Student *s = new Student();
// safely read file from the
ifile->read(reinterpret_cast<char *>(s), sizeof(Student));
while (!ifile->eof()) {
std::cout << s->whoami() << std::endl;
ifile->read(reinterpret_cast<char *>(s), sizeof(Student));
}
} else {
std::cerr << "Unable to open file\n";
}
ifile->close();
delete ifile;
delete ofile;
delete student;
ifile = nullptr;
ofile = nullptr;
student = nullptr;
return 0x0;
}