XD灬 2015-03-10 14:48 采纳率: 100%
浏览 3276
已采纳

vs2013 fstream 无法打开文件

include

//# include

include

using namespace std;

class student
{
public:
int num;
char name[20];
float score;
};
int main()
{
student std[5] = { 1001, "Li", 85, 1002, "Fun", 97.5, 1004, "Wang", 54, 1006, "Tan", 76.5, 1010, "Ling", 96 };
fstream iofile;
iofile.open("stud.dat", ios::in|ios::out|ios::binary);
if (!iofile)
{
cerr << "Error!" << endl;
exit(1);
}
for (int i = 0; i < 5; i++)
{
iofile.write((char *)&std[i], sizeof(std[i]));
}
student stud[5];
for (int i = 0; i < 5; i = i + 2)
{
iofile.seekg(i * sizeof(std[i]), ios::beg);
iofile.read((char *)&stud[i / 2], sizeof(stud[0]));
cout << stud[i / 2].num << ' ' << stud[i / 2].name << ' ' << stud[i / 2].score << endl;
}
cout << endl;
stud[2].num = 1012;
strcpy_s(stud[2].name, 20, "Wu");
stud[2].score = 60;
iofile.seekp(2 * sizeof(stud[0]), ios::beg);
iofile.write((char *)&std[2], sizeof(std[2]));
iofile.seekg(0, ios::beg);
for (int i = 0; i < 5; i++)
{
iofile.read((char *)&stud[i], sizeof(stud[i]));
cout << stud[i].num << ' ' << stud[i].name << ' ' << stud[i].score << endl;
}
iofile.close();
return 0;
}

输出的是Error,求解??看不出什么错误!

  • 写回答

4条回答 默认 最新

查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格