ifstream的问题,我的in_file文件和out_file文件应该自己建立嘛,健在那里

ergouzitadie 2003-09-14 12:06:38
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main()
{
ofstream outfile("out_file");
ifstream infile("in_file");

if(!infile){
cerr<<"error:unable to open input file!\n";
return -1;
}
if(!outfile){
cerr<<"error:unable to open output file!\n";
return -2;
}

string word;
while (infile>>word)
outfile<<word<<' ';

return 0;
}

程序如上
...全文
144 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ergouzitadie 2003-09-14
  • 打赏
  • 举报
回复
呵呵,我说那,我建了一个text文件所以程序没有成功,不过最后倒是生成了一个没有扩展名的out_file
wangfengsdu 2003-09-14
  • 打赏
  • 举报
回复
还有就是文件"in_file"是没有扩展名的,
这个要特别注意!
wangfengsdu 2003-09-14
  • 打赏
  • 举报
回复
我经过测试,
你的程序没有问题。
当你在develop studio环境下执行程序的话,
文件"in_file"应该建立在project所在的目录下。
如果你通过点击方式直接执行程序的话,
那么应该将文件"in_file"建立在project所在目录下
的debug目录下,经过测试,运行正确!
给分。。。
ergouzitadie 2003-09-14
  • 打赏
  • 举报
回复
我将input_file建在和dsw一样的文件夹下,或者建在下一级的debug目录下,程序运行结果都是
error:unable to open input file
press any key to continue
好像都是打不开
这个是c++primer上的小例子,帮个忙,谢谢
tuxw 2003-09-14
  • 打赏
  • 举报
回复
可以在建立时指定带绝对路径的文件名
不指定则在当前目录下
brxren 2003-09-14
  • 打赏
  • 举报
回复
那就在后面加个.txt就好了啊

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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