I wrote a program that reads information about students (first name, last name and points for three verification tests). Works, in principle. But when you exit the console when the program ends, the error takes off:
An exception was thrown: read access violation.
_Pnextwas0x114999C.
What does it mean? Below is the code of the program in which the place of the error is highlighted by comments.
#include "stdafx.h" #include <fstream> #include<iostream> #include <iomanip> #include <string> using namespace std; int main() { fstream stud; fstream bin_stud; stud.open("stud.txt", ios::in); bin_stud.open("a.dat", ios::app | ios:: binary); int col = 0; //подсчет количества студентов string name1; string surname1; int test11; int test12; int test13; while (!stud.eof()) { stud >> name1; stud >> surname1; stud >> test11; stud >> test12; stud >> test13; bin_stud.write((char *)&name1, sizeof(name1)); bin_stud.write((char *)&surname1, sizeof(surname1)); bin_stud.write((char *)&test11, sizeof(test11)); bin_stud.write((char *)&test12, sizeof(test12)); bin_stud.write((char *)&test13, sizeof(test13)); col++; } bin_stud.close(); stud.close(); bin_stud.open("a.dat", ios::in | ios:: binary); for (int i = 0; i < col; i++) //вот здесь я хочу вывести содержимое бинарного файла в консоль, все выводится, но после работы программы вылетает ошибка. Если же вывод убрать из кода, то все работает нормально { bin_stud.read((char *)&name1, sizeof(name1)); bin_stud.read((char *)&surname1, sizeof(surname1)); bin_stud.read((char *)&test11, sizeof(test11)); bin_stud.read((char *)&test12, sizeof(test12)); bin_stud.read((char *)&test13, sizeof(test13)); cout << name1 << " "; cout << surname1 << " "; cout << test11 << " "; cout << test12 << " "; cout << test13<< " "; cout << endl; } bin_stud.close(); system("pause"); return 0; }