[英]Reading and writing C++ objects into Random access files
我是C ++初學者,我想做的是讀寫在程序中創建的Staff對象。
下面是我的Write方法:
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中出現以下錯誤。
StaffPersonnelSystem.exe中0x53950E9A(msvcr110d.dll)的未處理異常:0xC0000005:訪問沖突讀取位置0x00F5BF28。
我似乎無法理解我哪里出了問題,如果有人可以幫助我處理此代碼,我將非常感激。
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
您不能將Staff類型的對象寫入文件,也不能回讀並期望它可以工作。 只有當您的班級使用POD(原始數據)時,這種情況才有可能。 您的Staff類不是POD-它包含字符串對象。 順便說一句, if (temp != NULL)
您測試if (temp != NULL)
是否錯誤-如果temp在讀取調用之前不為NULL,則在之后不會為NULL。
您犯了幾個錯誤。 首先是認為將位圖像寫入文件可以做任何事情。 程序外的所有數據都必須以某種方式進行格式化。 如果要使用二進制格式,則需要使用函數來格式化和解析int
和string
。 (除非有很強的理由這樣做,否則您可能應該使用現有格式:XDR可能是最簡單的格式。)
如果您的數據都是基本數據類型( int
, char[]
等),則似乎可以讀寫按位圖像。 直到您更改了編譯器選項或版本,或嘗試使用其他系統。 即使基本類型也需要格式化。 (當然,任何帶有指針的東西都需要格式化; std::string
幾乎可以肯定在其實現中帶有指針。)
最后,您可以使用staffDetailsFile.read
的結果,而無需測試讀取是否成功。 那是未定義的行為。 您的循環可能應該是while ( staffDetailsFile.read( ... ) )
。 (但這只會為您提供未格式化的緩沖區;您仍然需要提取數據。而且您甚至可能不知道要讀取多少字節,因為std::string
格式可能有一個可變長度。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.