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
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Student
{
char name[30];
int age;
int fn;
};
void initStudent(Student& st, const char* name, int age, int fn)
{
strcpy(st.name, name);
st.age = age;
st.fn = fn;
}
void saveToFile(const Student* students, size_t count, ofstream& file)
{
file.write((const char*)students, count * sizeof(Student));
}
int main()
{
Student* arr = new Student[4];
initStudent(arr[0], "ivan", 44, 1234);
initStudent(arr[1], "petur", 12, 765);
initStudent(arr[2], "alex", 15, 44);
initStudent(arr[3], "katerina", 19, 12134);
ofstream file("students.dat", ios::binary);
saveToFile(arr, 4, file);
delete[] arr;
}
|