【问题标题】:C++ Exception thrown: read access violation (fopen/fread struct)抛出 C++ 异常:读取访问冲突(fopen/fread 结构)
【发布时间】:2016-10-24 16:08:02
【问题描述】:

这是我的结构:

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 文件中时,我得到了这个错误:“抛出异常:读取访问冲突”。

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

【问题讨论】:

  • 什么TaiKhoan?
  • C 和 C++ 是不同的语言。选择你编译的那个并相应地编辑问题(包括标签)!
  • @PeteBecker:我不知道 C 有一个类型 string 并支持字符串文字上的加法运算符。

标签: c++ struct fopen fread


【解决方案1】:

您不能 freadstd::string(或包含 std::string 的对象),因为 std::string 对象仅包含指向实际字符串的指针(某些库实现的短字符串除外。)以前写的指针是没有意义的,使用指针是未定义的行为。

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2016-08-25
    • 2022-01-08
    • 2021-05-01
    • 2021-12-19
    相关资源
    最近更新 更多