请问下关于文件的操作

fdar 2009-02-23 09:50:47
在网上看到这样一个代码

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

fstream rs ;
rs.open("E:\\test.txt", iso::in);



if(rs.is_open()) {
cerr << "error: unable to open input file: "
<< rs << endl;
}

system("pause");
return 0;
}





但是编译不成功为什么呢,请问哪里有问题,还有它的原理
...全文
116 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
MutouMajia 2009-02-24
  • 打赏
  • 举报
回复
你没看编辑器的错误描述么?
ytfrdfiw 2009-02-24
  • 打赏
  • 举报
回复
如果你是在windows下做开发,最好使用ms提供的API来实现文件I/O,虽然以上的实现也是由CreateFileA/W,WriteFile,ReadFile来实现
mosaic 2009-02-24
  • 打赏
  • 举报
回复
从逻辑上看,if(rs.is_open()) 应该改为 if(!rs.is_open())

否则打开成功的时候打出日志是不成功。

herman~~ 2009-02-24
  • 打赏
  • 举报
回复
下次麻烦把编译提示也贴出来吧

rs.open("E:\\test.txt", ios::in);
szqh97 2009-02-24
  • 打赏
  • 举报
回复
ios
thesecretblue 2009-02-23
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

fstream rs ;
rs.open("E:\\test.txt", ios::in);



if(rs.is_open()) {
cerr << "error: unable to open input file: " << rs << endl;
}

system("pause");
return 0;
}

除了上述,还有cerr不能输出文件流对象。
laocpp 2009-02-23
  • 打赏
  • 举报
回复
iso::in 改成 ios::in, 应是笔误. 不要忘了 "rs.close();" 关闭打开的文件.
原理是用STL的文件流来操作文件的读写, 详细请参阅 fstream (例MSDN) 帮助.
fdar 2009-02-23
  • 打赏
  • 举报
回复
谢谢,顺便能把读文件,写文件等等列下,不要网那一答堆的那种。。。
pengzhixi 2009-02-23
  • 打赏
  • 举报
回复
iso 2000认证
waizqfor 2009-02-23
  • 打赏
  • 举报
回复
改完了 试试

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

fstream rs ;
rs.open("E:\\test.txt", ios::in);



if(rs.is_open()) {
cerr << "error: unable to open input file: "
<< rs << endl;
}

system("pause");
return 0;
}
mosaic 2009-02-23
  • 打赏
  • 举报
回复
iso::in
改为
ios::in
mosaic 2009-02-23
  • 打赏
  • 举报
回复
io::in
改为
ios::in

65,210

社区成员

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

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