Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 03 / students_staticName_to_binary.cpp
blob: 8f84d79ef2add0699cd021f4800a6937d8f4938a (plain) (blame)
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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;


struct Student
{
    char name[25];
    int fn;
    int age;
};

int main()
{
    { //write
        Student st;
        strcpy(st.name, "Ivan");
        st.fn = 1234;
        st.age = 33;
        
        ofstream file("student.dat", ios::binary);
        
        file.write((const char*)&st, sizeof(st));
    }
    
    { //read
        Student st;
        ifstream file("student.dat", ios::binary);

        file.read((char*)&st, sizeof(st));
        
        std::cout << st.name << " " << st.fn << " " << st.age << std::endl;
    }
    return 0;
}