mardi 24 mars 2015

c++ throws 0xC0000005: Access violation writing location 0xFEEEFEEE. on return from main

program creates binary random access file "student.dat" populates file with 6 records then closes the file. then the file is opened read/write then the 6 records are formatted and displayed. then several records are displayed then updated then displayed again to verify updates "took". then the file is closed. when the return is reached that is when the access violation writing location... is generated. The error pointed to by the debugger is in a system file called xutility line 217. I have no idea what xutility does. my question is what am I doing to cause this error after the program runs and performs all that it is supposed to do.


Here is my main code:



/*******************************************************************************
* CIS-278 *
* *
* Program Name: Student_Main_Driver.cpp *
* *
* Author: Don Register *
* *
* Written: March 2015 *
* *
* Purpose: Demonstrate the use of random access files. *
* *
* Modifications: *
* *
*******************************************************************************/

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include "Student.h"

using namespace std;

void displayOutput( Student &);

int main(int argc, char *argv[])
{
fstream studentFile;
studentFile.open("student.dat", ios::out | ios::binary);

Student student01(1100, "Washington", "George", "Mil", 40, 3.77);
Student student02(1200, "Jefferson ", "Thomas", "Gov", 45, 3.88);
Student student03(1300, "Adams ", "John ", "Psy", 39, 3.79);
Student student04(1400, "Franklin ", "Ben ", "Sci", 50, 3.89);
Student student05(1500, "Hamilton ", "Alex ", "Pol", 42, 3.85);
Student student06(1600, "Hancock ", "John ", "Pub", 48, 3.81);

studentFile.write(reinterpret_cast<char*>(&student01),sizeof(Student));
studentFile.write(reinterpret_cast<char*>(&student02),sizeof(Student));
studentFile.write(reinterpret_cast<char*>(&student03),sizeof(Student));
studentFile.write(reinterpret_cast<char*>(&student04),sizeof(Student));
studentFile.write(reinterpret_cast<char*>(&student05),sizeof(Student));
studentFile.write(reinterpret_cast<char*>(&student06),sizeof(Student));

studentFile.close();

studentFile.open("student.dat", ios::in | ios::out | ios::binary);

Student studentNew;

studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.seekg(1 * sizeof(Student), ios::beg);
studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.seekg(2 * sizeof(Student), ios::beg);
studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);
studentNew.setStudentMajorCode("CIS");
studentFile.seekp(2 * sizeof(Student), ios::beg);
studentFile.write(reinterpret_cast<char*>(&studentNew), sizeof(Student));
studentFile.seekg(2 * sizeof(Student), ios::beg);
studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.seekg(3 * sizeof(Student), ios::beg);
studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);
studentNew.setStudentCreditsEarned(55);
studentFile.seekp(3 * sizeof(Student), ios::beg);
studentFile.write(reinterpret_cast<char*>(&studentNew), sizeof(Student));
studentFile.seekg(3 * sizeof(Student), ios::beg);
studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.seekg(4 * sizeof(Student), ios::beg);
studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);
studentNew.setStudentGPA(3.91);
studentFile.seekp(4 * sizeof(Student), ios::beg);
studentFile.write(reinterpret_cast<char*>(&studentNew), sizeof(Student));
studentFile.seekg(4 * sizeof(Student), ios::beg);
studentFile.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));
displayOutput(studentNew);

studentFile.close();

cout << "\n\nPress Enter to continue. . .";
char ch = getchar();

return EXIT_SUCCESS; // this is where the error is generated

} // end of main

void displayOutput(Student &studentNew)
{
string outPut;
outPut = studentNew.toString();
cout << outPut;
}

Aucun commentaire:

Enregistrer un commentaire