注册 登录
编程论坛 C++教室

二进制文件流的打开问题

非己莫属 发布于 2012-05-17 16:00, 645 次点击
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    fstream data;
    data.open("student",ios::in|ios::out|ios::binary);
    if(!data)
{
     cerr<<"file open error"<<endl;
     return -1;
}
data.close();
return 0;
}
为什么程序运行显示  file open error  ,怎么修改才使它能打开。
5 回复
#2
ZJQLOVELYY2012-05-17 22:20
我觉得你应该去查下data.open("student"中这个student文件的后缀如student.txt或student.dat,,,dat好像是二进制文件的后缀
#3
rjsp2012-05-18 08:34
ios::in|ios::out|ios::binary 相当于 fopen 的 "r+b",也就是你这个文件必须存在,否则报错
fopen 的 "w+b" 则对应 ios::in|ios::out|ios_base::trunc|ios::binary,你试试看
#4
天使梦魔2012-05-18 08:45
程序没问题

如果你的student是个txt文件的话,就用data.open("student.txt",ios::in|ios::out|ios::binary);
文件类型必须写全,除非你建个不带类型的文件叫这个名字
#5
非己莫属2012-05-19 17:48
楼上,我又重新加后缀后,不管是"student.txt",还是"student.dat",结果还是file open error呀,为什么呀
#6
邋遢鬼2012-05-29 08:44
您这种打开方式首先这个文件在exe的目录存在。
1