C++抛出异常:读取访问冲突(的fopen/FREAD结构)

问题描述:

这里是我的结构:C++抛出异常:读取访问冲突(的fopen/FREAD结构)

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文件。

请帮我解决这个错误,非常感谢你的帮助!

+2

What'' TaiKhoan'? – tkausl

+0

C和C++是**不同的**语言。选择你编译的那个,并相应地编辑问题(包括标签)! – Olaf

+1

@PeteBecker:我不知道C有一个'string'类型,并支持字符串文字上的加法运算符。 – Olaf

不能fread一个std::string(或含有std::string一个对象)时,由于std::string对象只包含指针的实际字符的字符串(除了用一些库实现短字符串。)读先前写的指针是无意义并使用指针是未定义的行为。