I am new to your forum, so please forgive any missteps. I am working on a c++ project that reads and writes to a binary file. I first tried doing this using full on c++ but when an error popped up, my instructor told me to use c style file manipulation. Low and behold, I get the same error:
Unhandled exception at 0x6087CCC8 (msvcp110d.dll) in CSI_FinalProj_EmployeeDB.exe: 0xC0000005: Access violation reading location 0x00CDDAEC.
This occurs after successfully completing the read and print, and successfully closing the file. It always occurs when the program exits the function and attempts to return to the calling function. If I put it in the main, it blows up after the return, when the program ends.
The function is a simple print function:
void fileClerkType::printRecord(int id)const
{
FILE* spRead;
employeeType record;
long location;
long size;
location = id - 1;
size = sizeof(employeeType);
spRead = fopen("companyFile.dat", "r");
fseek(spRead, location*size, SEEK_SET);
fread(&record, sizeof(employeeType), 1, spRead);
// If a record has been deleted, the id will be 0
// In that case, don't print
if (record.getEmployeeID() != 0)
{
cout << record << endl;
fread(&record, sizeof(employeeType), 1, spRead);
}
fclose(spRead);
}//Unhandled exception at 0x5065CCC8 (msvcp110d.dll) in
//CSI_FinalProj_EmployeeDB.exe: 0xC0000005: Access violation
//reading location 0x00CDDAEC.
As I said, the function works perfectly. employeeType is a class that has:
2 ints, three strings, and a float
Here is the original c++ version with the same problem. The only difference is that this prints all of the records. It also works perfectly.:
void administratorType::showAllRecords()
{
long test;
long position = 0;
long recordSize = sizeof(employeeType);
ifstream inFile("EmployeesNew.dat", ios::in | ios::binary);
employeeType buffer; // empty employeeType
if(inFile.is_open())
{
inFile.seekg((position * recordSize), ios::beg);
test = inFile.peek(); // Debug
inFile.read(reinterpret_cast<char*>(&buffer), recordSize);
position = 0;
while(position < getRecordCount())
{
inFile.seekg((position * recordSize), ios::beg);
test = inFile.peek();
inFile.read(reinterpret_cast<char*>(&buffer), recordSize);
outputRecord(cout, buffer);
position++;
}
inFile.close();
}
}// Runs fine to here, but throws error when leaving the function
// Unhandled exception at 0x5408CCC8 (msvcp110d.dll) in
// ProjectName.exe: 0xC0000005: Access violation
// reading location 0x0137D3B4.
It has to be an implementation issue. But I cannot see it. Is there something in the implementation that is causing the pointers keeping track of function calls and returns to be corrupted? Thank you in advance for your help.
Sorry, here is the list of member variables for the Employee class. They are not fixed length strings:
int age;
int employeeID; // Auto-generated
float salary;
string lastName;
string firstName;
string ssn;