关于文件流的问题

shj1977 2009-06-21 11:52:02
#include<iostream>
#include<fstream>
using namespace std;
class Animal
{
public:
Animal(int age) {itsage=age;}
~Animal() {};
int getage() const {return itsage;}
private:
int itsage;
};
char strone[250];
Animal bearone(10);
Animal beartwo(20);
char ch;
int main()
{
ofstream fout(strone,ios::binary);
if(fout)
{
cout<<"true"<<endl;

}
fout.write((char*) &bearone,sizeof(bearone));
fout.close();
ifstream fin(strone,ios::binary);
if(fin)
{
cout<<"yes"<<endl;

}
cout<<strone<<endl;
fin.read((char*) &beartwo,sizeof(beartwo));
fin.close();
cout<<beartwo.getage()<<endl;
return 0;
}

编译能通过,可是运行后,出现:
Debug assertion failed!
不能运行,请问这是怎么回事?
代码应该如何改?才能云行?
谢谢!!!!!!!!!!!!!1
...全文
34 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shj1977 2009-06-21
  • 打赏
  • 举报
回复
可以了
谢谢
shj1977 2009-06-21
  • 打赏
  • 举报
回复
改了之后,还是不能运行
shj1977 2009-06-21
  • 打赏
  • 举报
回复
试试
谢谢
liao05050075 2009-06-21
  • 打赏
  • 举报
回复
当然要存在啦。
你可以指定任何一个文件给它打开,比如你的C盘里有abc.txt ,你就写"c:\\abc.txt"
shj1977 2009-06-21
  • 打赏
  • 举报
回复
in txt 这个文件是不是一定得存在?
liao05050075 2009-06-21
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
using namespace std;
class Animal
{
public:
Animal(int age) {itsage=age;}
~Animal() {};
int getage() const {return itsage;}
private:
int itsage;
};
char strone[250];
Animal bearone(10);
Animal beartwo(20);
char ch;

int main()
{
strcpy(strone,"in.txt");//in.txt是你要打开的文件名
ofstream fout(strone,ios::binary); //strong里面没东西,到这里自然会出错误
if(fout)
{
cout <<"true" <<endl;

}

fout.write((char*) &bearone,sizeof(bearone));
fout.close();
ifstream fin(strone,ios::binary);
if(fin)
{
cout <<"yes" <<endl;

}
cout <<strone <<endl;
fin.read((char*) &beartwo,sizeof(beartwo));
fin.close();
cout <<beartwo.getage() <<endl;
return 0;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧