Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 03 / array_of_students_write.cpp
blob: aa11ce3310369e094a4176ee8206582da30b4bbe (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
38
39
40
41
42
43
44
45
46
47
48
#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);
		
	if(!file.is_open())
	{
		std::cout << "Error while opening the file!" << std::endl;
		delete[] arr; //!
		return -1;
	}
		
	saveToFile(arr, 4, file);
	
	delete[] arr;
	
	return 0;
}