Every time I run this it returns -1073741819 (0xC0000005) and doesn't show the name and id and I already inserted a name and id in the test.dat file.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
struct student
{
string name;
int id;
}
int main()
{
student stud;
fstream file1;
file1.open("test.dat",ios::in|ios::binary);
if(pol.fail()){
cerr<<"an error occurred"<<endl;
exit(1);
}
file1.seekg(0);
while(file1.read((char*) &stud,sizeof(stud))){
// *already written a txt in test.dat file*
cout<<stud.name<<endl;
cout<<stud.id<<endl;
}
file1.close();
}
User contributions licensed under CC BY-SA 3.0