Access violation Error on FStream

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#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..
Topic archived. No new replies allowed.