Began to learn С++ . There is a code:
#include <iostream> #include <fstream> using namespace std; struct student { string name; int year; int course; }; int main() { student st1; cout << "Имя студента: "; cin >> st1.name; cout << "Год рождения: "; cin >> st1.year; cout << "Курс: "; cin >> st1.course; FILE *f; f = fopen("111.dat", "wb"); fwrite(&st1, sizeof(student), 1, f); fclose(f); return 0; } It's all clear. Add:
student st2; FILE *f2; f2 = fopen("111.dat", "rb+"); fread(&st2, sizeof(student), 1, f2); fclose(f2); cout << "Студент " << st2.name << ", " << st2.year << " года рождения учится на " << st2.course << " курсе."; and we get the error "*** Error in` / home / artik / Documents / QTProjects / build-untitled-Desktop-Release / untitled ': free (): invalid pointer: 0xbfe10d44 ** "(OS - Ubuntu). Although after the text of the error displays the correct result. And here's the code
student *st2 = new student(); FILE *f2; f2 = fopen("111.dat", "rb+"); fread(st2, sizeof(student), 1, f2); fclose(f2); cout << "Студент " << st2->name << ", " << st2->year << " года рождения учится на " << st2->course << " курсе."; It does not cause an error. Although both options, in theory, should work. Help to understand, please!