#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; struct Worker { short id; int age; double salary; }; int main() { Worker* p1 = new Worker[100]; Worker* p2 = new Worker[100]; p1[0].id = 1; p1[0].age = 20; p1[0].salary = 1200; p1[1].id = 2; p1[1].age = 32; p1[1].salary = 1320; // Запис FILE* outfile; outfile = fopen("1.txt", "w"); if (outfile == NULL) { cout << "File error" << endl; return 1; } fwrite(p1, sizeof(struct Worker), 2, outfile); // 2 елементи fclose(outfile); // Читання FILE* infile; infile = fopen("1.txt", "r"); if (infile == NULL) { cout << "File error" << endl; return 1; } int n = 0; while (fread(&p2[n], sizeof(struct Worker), 1, infile)) { n++; } cout << "Elements: " << n << endl << endl; fclose(infile); for (int i = 0; i < n; i++) { cout << p2[i].id << endl; cout << p2[i].age << endl; cout << p2[i].salary << endl; cout << endl; } system("pause"); return 0; }
Elements: 2
1
20
1200
20
1200
2
32
1320
32
1320
Додаткова структура як БД
struct DB { int size; Worker w[100]; }; DB t; fwrite(&t, sizeof(t), 1, outfile); fread(&t, sizeof(t), 1, infile);