我是C 初学者,我想做的是读和写我在程序中创建的员工对象。
以下是我的写作方法:
void Staff::writeStaffFile(){
const int vectorSize = staffList.size();
ofstream staffDetailsFile("staffDetails.txt", ios::out | ios::binary);
if (!staffDetailsFile){
cerr << "nFile open error - Error writing staff details" << endl;
return;
}
for (int i=0; i<vectorSize; i++){
staffDetailsFile.write(reinterpret_cast< const char* >(&staffList[i]), sizeof(Staff));
}
staffDetailsFile.close();
}
工作人员对象保存在向量中,在这里,我试图将矢量中可用的所有人员对象保存到文件中。它有效并将数据写入文件中。
我出错的地方是阅读文件。这是我的阅读方法:
void Staff::readStaffFile(){
ifstream staffDetailsFile("staffDetails.txt", ios::in | ios::binary);
if (!staffDetailsFile)
cerr << "nFile open error - Staff details not found" << endl;
else {
Staff *temp = (Staff *)malloc(sizeof(Staff));
while(!staffDetailsFile.eof()){
staffDetailsFile.read(reinterpret_cast<char *>(temp),sizeof(Staff));
if (temp != NULL)
Staff::insertAccount(temp);
}
}
}
当我运行此部分时,我会在Visual Studio中获得以下错误。
suffSernelsystem.exe中的0x53950E9A(MSVCR110D.DLL)的未经处理的例外
我似乎不明白我在哪里出了问题,如果有人可以帮助我处理此代码,我会很奇怪。
ps:这是我的员工课程定义:
#include <iostream>
#include <string>
#include <malloc>
#include <vector>
#include "Person.h"
#ifndef STAFF_H
#define STAFF_H
class Staff : public Person {
public:
Staff(int const, string,string,int,string,string,char,Designation,Department,Date,string,string,Date,bool); //staff constructor
//set methods
void setStaffIDNumber(int const);
void setUsername(string);
void setPassword(string);
void setAccessLevel(int);
//edit, modify other staff accounts
static void addStaff(int);
static int generateStaffID();
static void deleteStaff(int, Staff*);
static void changePassword(Staff*);
static bool modifyStaff(int, Staff*);
static void insertAccount(Staff*);
static void printStaffDetails(Staff*);
static void writeStaffFile();
static void readStaffFile();
static bool isValidAccount(Staff*,string, string);
static Staff* chooseStaffAccount();
static void searchStaff();
static void refreshVector();
//get methods
Staff getStaffAccount(string);
int getAccessLevel();
string getUserName();
int getStaffID();
string getPassword();
//search staff accounts
static Staff* searchStaffAccount(string); //search staff accounts by userName
static Staff* searchByID(int); //search staff accounts by ID
static void searchByDept(Department); //Get staff registered to perticular department
static void searchByDesignation(Designation); //Get staff registered to perticular designation
static void sortVector();
static bool sortByID(Staff &lhs, Staff &rhs);
static bool isVectorEmpty();
private:
int staffIDNumber;
string userName;
string passWord;
int accessLevel;
};
#endif
您犯了几个错误。首先是认为您可以用写入文件的位图像做任何事情。全部程序之外的数据必须以某种方式格式化。如果你想要二进制格式,您需要功能才能格式和解析int
和string
。(除非有一些有力的理由否则,您可能应该使用现有格式:XDR可能是最简单的。)
如果您的数据是所有基本数据类型(int
,char[]
等),写作和阅读位图像似乎似乎是工作。直到您更改编译器选项或版本或尝试过使用另一个系统。甚至基本类型都需要格式化。(和当然,任何带有指针的东西都需要格式化; std::string
几乎肯定有指针实施。)
最后:您使用staffDetailsFile.read
的结果不测试读取是否成功。那是不确定的行为。您的循环可能应该是while
( staffDetailsFile.read( ... ) )
。(但这只会让你未形式的缓冲区;您仍然需要提取数据。而且您甚至可能都不知道要阅读多少个字节,由于您的std::string
格式可能具有变量长度。)
您不能将其写入文件的对象,请阅读并期望它可以工作。只有当您的班级(普通旧数据)时,才有可能。您的员工课程不是吊舱 - 它包含字符串对象。顺便说一句,您的测试if (temp != NULL)
是错误的 - 如果在读取呼叫之前的临时时间不是null,则不会在其之后null。