程序创建二进制随机访问文件“student.dat”用6条记录填充文件然后关闭文件。然后打开文件读/写,然后格式化并显示6条记录。然后显示几条记录然后更新然后再次显示以验证更新“已接受”。然后文件关闭。当达到返回时,即生成访问冲突写入位置....调试器指向的错误位于一个名为xutility line 217的系统文件中。我不知道xutility的作用。我的问题是我在程序运行后如何导致此错误,并执行它应该执行的所有操作。
添加注释:仅在打开文件进行输入时才会出现此错误。即ios :: in
这是我的主要代码:
/*******************************************************************************
* 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;
}
这是我的student.h档案
// Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
class Student
{
public:
Student(); // default constructor
Student(int, string, string, string, int, double); // constructor with parameters
~Student(); // destructor
// Setter methods
void setStudentID(int);
void setStudentLName(string);
void setStudentFName(string);
void setStudentMajorCode(string);
void setStudentCreditsEarned(int);
void setStudentGPA(double);
// Getter methods
int getStudentID();
string getStudentLName();
string getStudentFName();
string getStudentMajorCode();
int getStudentCreditsEarned();
double getStudentGPA();
string toString();
private:
int studentID;
string studentLName;
string studentFName;
string studentMajorCode;
int studentCreditsEarned;
double studentGPA;
};
#endif
答案 0 :(得分:2)
static_assert( std::is_pod<Student>::value, "reading/writing as bytes is only safe and valid if you are a pod type" );
如果将其插入代码中,可能会失败。
产生的错误将解释你做错了什么。
序列化非pod类型需要做的不仅仅是重新解释转换。您通常可以通过这种方式对pod类型进行分类(假设结构大小保持不变)。非托管类型(如std::string
)需要自定义序列化/反序列化代码。