C++创建文件失败???

woshiwoxinheqiu 2009-03-17 04:34:06
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
class flei
{
private:
int x;
double y;
public:
friend ostream & operator<<(ostream &os,flei & ff);
friend istream & operator>>(istream &is,flei & ff);
void get(int q);
void display();
};
ostream & operator<<(ostream &os,flei & ff)
{
os.write((char *)& ff.x,4);
os.write((char *)& ff.y,8);
return os;
}
istream & operator>>(istream &is,flei & ff)
{
is.read((char *)&ff.x,4);
is.read((char *)&ff.y,8);
return is;
}
void flei::get(int q)
{
x=q;
y=sqrt(double(q));
}
void flei::display()
{
cout<<x<<y;
}
int main()
{
int a ;
fstream f1;
f1.open("dat.txt",ios::binary|ios::in|ios::out);
if(!f1)
{
cout<<"error"<<endl;
exit(0);
}
flei s;
for(int i=1;i<21;i++)
{
s.get(i);
f1<<s;
}
cout<<"请输入1-20内的数"<<endl;
cin>>a;
f1.seekg(sizeof(flei)*a);
f1>>s;
s.display();
f1.close();
return 0;
}

原题:
建立一个二进制文件,用来存放自然数1~~20及其平方根,然后输入1~~20之内的任意一个数,查找其平方根显示在屏幕上;
上面是我写的代码,谁帮我看下,我每次运行都是“error”
郁闷!
谁能给我说说为什么啊?
...全文
848 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuizhiyun 2009-03-18
  • 打赏
  • 举报
回复
文件不存在
woshiwoxinheqiu 2009-03-18
  • 打赏
  • 举报
回复
我中午试试看,先上课在说!
谢谢!
jackzhhuang 2009-03-17
  • 打赏
  • 举报
回复
对了

_creat和_S_IREAD | _S_IWRITE需要

#include<io.h>
#include <sys/stat.h>

jackzhhuang 2009-03-17
  • 打赏
  • 举报
回复
f1.open("D:\\dat.txt",ios::binary|ios::in|ios::out);

指定这个ios::in时文件必须存在,你去掉这个ios::in就可以了。

另外,你可以这样:

//先用这个创建文件
int nDes = _creat("D:\\dat.txt", _S_IREAD | _S_IWRITE);

//再关掉文件
_close(nDes);

//再用fstream用读写状态打开
fstream f1;
f1.open("D:\\dat.txt",ios::binary|ios::in|ios::out);
xinjun091 2009-03-17
  • 打赏
  • 举报
回复
都是高手了啊,俺刚入门,只能看点懂,以后慢慢请教啊!
woshiwoxinheqiu 2009-03-17
  • 打赏
  • 举报
回复
各位大侠,可不可以详细点啊,
真的很郁闷耶!
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
class flei
{
private:
int x;
double y;
public:
friend ostream & operator<<(ostream &os,flei & ff);
friend istream & operator>>(istream &is,flei & ff);
void get(int q);
void display();
};
ostream & operator<<(ostream &os,flei & ff)
{
os.write((char *)& ff.x,4);
os.write((char *)& ff.y,8);
return os;
}
istream & operator>>(istream &is,flei & ff)
{
is.read((char *)&ff.x,4);
is.read((char *)&ff.y,8);
return is;
}
void flei::get(int q)
{
x=q;
y=sqrt(double(q));
}
void flei::display()
{
cout<<x<<y;
}
int main()
{
int a ;
fstream f1;
f1.open("D:\\dat.txt",ios::binary|ios::in|ios::out);
if(!f1)
{
cout<<"error"<<errno<<endl;
exit(0);
}
for(int i=1;i<21;i++)
{
flei a;
a.get(i);
f1<<a;
}
cout<<"请输入1-20内的数"<<endl;
cin>>a;
f1.seekg(sizeof(flei)*a);
f1>>a;
f1.close();
return 0;
}

得不到想要的结果了,我花了很长时间来写这个的。
woshiwoxinheqiu 2009-03-17
  • 打赏
  • 举报
回复

VS2008很吃我CPU的,
但是VC6的话就很好说了,不占我资源!
要是VC6.0很标准的话,我也不用到VS2008了。
jackzhhuang 2009-03-17
  • 打赏
  • 举报
回复
在watch窗口输入
@err,hr
看看返回的错误是什么情况。

ps:2008玩这个不算大材小用了。
Dinelgua 2009-03-17
  • 打赏
  • 举报
回复
时文件位置放的不对,系统找不到
可以尝试f1.open("D:\\dat.txt",ios::binary|ios::in|ios::out);

我调试是ok的



f1.open("dat.txt",ios::binary|ios::in|ios::out);

if(!f1)
{
cout <<"error errono = "<< errno <<endl;
exit(0);
}
woshiwoxinheqiu 2009-03-17
  • 打赏
  • 举报
回复
对了,我用的是VS2008编译。
呵呵,有点大才小用!

65,187

社区成员

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

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