[英]C++ Exception thrown: read access violation (fopen/fread struct)
這是我的結構:
struct Account {
string accID;
string name;
float balance;
string currency;
int status;
};
功能:
Account layFileTaiKhoan(string id) {
FILE * openfile;
Account getAcc;
string mypath = "Account\\" + id + ".dat";
openfile = fopen(mypath.c_str(), "r");
fread(&getAcc, sizeof TaiKhoan, 1, openfile);
fclose(openfile);
return getAcc;
}
但是,當我使用函數將結構保存在.dat文件中時,出現了以下錯誤消息:“拋出了異常:讀取訪問沖突”。
請幫助我解決此錯誤,並非常感謝您的幫助!
您無法fread
std::string
(或包含std::string
的對象),因為std::string
對象僅包含指向實際字符串的指針(某些庫實現中的短字符串除外)。編寫的指針是沒有意義的,使用該指針是未定義的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.