File tree 5 files changed +86
-1
lines changed
5 files changed +86
-1
lines changed Original file line number Diff line number Diff line change
1
+ project (BinaryFile)
2
+
3
+
4
+ add_executable (${PROJECT_NAME} main.cpp student.hpp student.cpp)
Original file line number Diff line number Diff line change
1
+ #include " student.hpp"
2
+ #include < fstream>
3
+ #include < ios>
4
+ #include < iostream>
5
+ #include < string>
6
+
7
+ int main (int argc, char **argv) {
8
+ if (argc < 3 ) {
9
+ std::cerr << " usage: " << argv[0 ] << " <name> <age>\n " ;
10
+ return 0x0 ;
11
+ }
12
+
13
+ std::ofstream *ofile = new std::ofstream ();
14
+ std::ifstream *ifile = new std::ifstream ();
15
+
16
+ ofile->open (" student.dat" , std::ios::binary | std::ios::trunc );
17
+
18
+ // setup student object
19
+ Student *student = new Student ();
20
+ student->setAge ((unsigned short )std::stoul (argv[2 ]));
21
+ student->setName (argv[1 ]);
22
+
23
+ ofile->write (reinterpret_cast <char *>(student), sizeof (*student));
24
+ ofile->close ();
25
+
26
+ ifile->open (" student.dat" , std::ios::binary);
27
+ if (ifile->is_open ()) {
28
+ Student *s = new Student ();
29
+ while (!ifile->eof ()) {
30
+ ifile->read (reinterpret_cast <char *>(s), sizeof (Student));
31
+ std::cout << s->whoami () << std::endl;
32
+ }
33
+ } else {
34
+ std::cerr << " Unable to open file\n " ;
35
+ }
36
+
37
+ ifile->close ();
38
+
39
+ delete ifile;
40
+ delete ofile;
41
+ delete student;
42
+ ifile = nullptr ;
43
+ ofile = nullptr ;
44
+ student = nullptr ;
45
+
46
+ return 0x0 ;
47
+ }
Original file line number Diff line number Diff line change
1
+ #include " student.hpp"
2
+ #include < sstream>
3
+ #include < string>
4
+
5
+ void Student::setAge (int age) { this ->age = age; }
6
+ void Student::setAge (short age) { this ->age = age; }
7
+ void Student::setAge (unsigned short age) { this ->age = age; }
8
+
9
+ void Student::setName (std::string &name) { this ->name = name; }
10
+ void Student::setName (const char *name) { this ->name = std::string (name); }
11
+
12
+ std::string Student::whoami () const {
13
+ std::stringstream s;
14
+ s << " I am " << this ->name << " and " << this ->age << " years old" ;
15
+ return s.str ();
16
+ }
Original file line number Diff line number Diff line change
1
+ #include < string>
2
+
3
+ class Student {
4
+ private:
5
+ std::string name;
6
+ unsigned short age;
7
+
8
+ public:
9
+ void setAge (int age);
10
+ void setAge (short age);
11
+ void setAge (unsigned short age);
12
+
13
+ void setName (std::string &name);
14
+ void setName (const char *name);
15
+
16
+ std::string whoami () const ;
17
+ };
Original file line number Diff line number Diff line change @@ -10,4 +10,5 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
10
10
11
11
add_subdirectory (WritingFile)
12
12
add_subdirectory (ReadingFile)
13
- add_subdirectory (AppendingFile)
13
+ add_subdirectory (AppendingFile)
14
+ add_subdirectory (BinaryFile)
You can’t perform that action at this time.
0 commit comments