Firstly, Here's my code running in VS 2010.

#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;


class Student
{
	friend ostream& operator<<(ostream&, Student);
	friend istream& operator>> (istream&, Student&);

private:
	int stuId;
	string name;
	int gpa;

public:
	Student(int, string, int);
	int getStudId();
};

ostream& operator<<(ostream& out, Student stu)
{
	out<<stu.stuId<< " " << stu.name<< " " << stu.gpa<<endl;
	return out;
}

istream& operator >> (istream& in, Student& stu)
{
	in >> stu.stuId >> stu.name >> stu.gpa;
	return in;
}


Student::Student(int id=0, string nm = "", int gpa = 0.0)
{
	this->stuId = id;
	this->name =nm;
	this->gpa = gpa;
}

int Student::getStudId()
{
	return stuId;
}

int main()
{

	const int QUIT = 0;
	int idNum;
	string name;
	int gpa;

	cout <<"Enter student ID number or " << QUIT << " to quit ";
	cin >>idNum;

		cout << "Enter Name ";
		cin >> name;
		cout << "Enter GPA ";
		cin >> gpa;
		Student aStudent(idNum, name, gpa);

	fstream outfile;
	outfile.open("Student3.dat", ios::app |ios::out | ios::binary);

	outfile.write(reinterpret_cast <const char*>(&aStudent), sizeof(Student));
	outfile.flush();
	
	outfile.flush();
	outfile.close();
	system("pause");
	Student aStudent2;
	fstream infile;
	
	infile.open("Student3.dat", ios::in | ios::binary);

	infile.read(reinterpret_cast <char*>(&aStudent2), sizeof(Student));

	//infile.seekg((0 -1) * sizeof(aStudent));

	infile.flush();
	infile.close();


		cout<< aStudent2 << endl;
	
	system("pause");
	return 0;
}

My program run just fine until the end of program, return 0, and it throw this error

First-chance exception at 0x59b4ad4a (msvcp100d.dll) in file reader and writer.exe: 0xC0000005: Access violation reading location 0x002a4d04.
Unhandled exception at 0x59b4ad4a (msvcp100d.dll) in file reader and writer.exe: 0xC0000005: Access violation reading location 0x002a4d04.

May i know what did i do wrong?
Thanks in advance..

sorry..

I cant seems to edit/delete my first post.. Hope staff/moderator here would kind enough to remove it for me.. Thanks..

Edited 6 Years Ago by gallantlex: n/a

This article has been dead for over six months. Start a new discussion instead.