这个程序为什么闪一下就没了?

yangkunhenry 2008-08-02 09:53:41
#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!"<<endl;
return -1;
}
if(!outfile)
{
cerr<<"error:unable to open outfile file!"<<endl;
return -1;
}
string word;
while(infile>>word)
outfile<<word<<' ';

return 0;
}

小弟刚学c++,在C++Primer上抄了个程序,运行一下一闪就没了……
...全文
454 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
haohaokingXP 2008-08-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ttkk_2007 的回复:]
最后加上system("pause");
[/Quote]
visame 2008-08-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yangkunhenry 的回复:]
不是你们想的那样
infile>>word这句压根儿就没执行啊!
[/Quote]
我怀疑是你的infile本身就是空的,里面什么东西都没有。
  • 打赏
  • 举报
回复
问题在于:1,in_file这个文件没有创建,进行if(!infile)检查时就直接return -1;
out_file创建对象是会自行创建!!
error:unable to open input file!
Press any key to continue
2,对于一闪而过的问题,在vc6下程序调试正常!并没有一闪而过!
如果你出来这种情况,建意使用system("pause");

处理方法1,自行新建一个TXT的文件,并改名为in_file (注意后面的扩展名).
或是直接修改程序为ifstream infile("in_file.txt");


好象楼上都把问题给解答了......呵呵,我就当是路过吧!!
kgduwu 2008-08-03
  • 打赏
  • 举报
回复
楼主刚在VC上试了下你的程序,将"out_file"改为"a.txt",将"in_file"改为"兼职.txt",(兼职.txt在调试的文件夹里存在并且有内容)运行结果是程序将兼职.txt的内容拷贝并覆盖了a.txt的内容,说明该程式并无问题,问题出在你的"in_file"不存在,导致infile>>word就没执行,即if(!infile)
{
cerr<<"error:unable to open input file!"<<endl;
return -1;
}
直接退出了,
第二种情况是,程序实际执行了,并且执行的很好,只是一闪而过你没看到结果而已,那可能是你的编译器不是VC,而用你用的编译器还必须下面的暂停代码之一,才能看到结果
system("pause");
//or
//getchar();
return 0;

楼主再试试看!

lsm164 2008-08-03
  • 打赏
  • 举报
回复
还是不行啊,一运行就闪下没了infile>>word就没执行。

闪一下就没了是正常的,说明执行很快。
关键是打开out_file.txt文件,看看是否把in_file.txt文件中的字符串拷贝过去了。
elegant87 2008-08-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ttkk_2007 的回复:]
最后加上system("pause");
[/Quote]
如果我知道 2008-08-02
  • 打赏
  • 举报
回复
你用的是Dev-C++吧,加上system("pause")就好了;
jinwei1984 2008-08-02
  • 打赏
  • 举报
回复
你也可以用你的程序
先创建两个 out_file.txt,in_file.txt
然后去掉后缀名txt
jinwei1984 2008-08-02
  • 打赏
  • 举报
回复

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ofstream outfile("out_file.txt"); //自己创建两个文本文件,你那个是二进制格式
ifstream infile("in_file.txt"); //
if(!infile)
{
cerr<<"error:unable to open input file!"<<endl;
return -1;//这里返回退出函数了
}
if(!outfile)
{
cerr<<"error:unable to open outfile file!"<<endl;
return -1;
}
string word;
while(infile>>word)
outfile<<word<<' ';

system("pause");
return 0;
}
yangkunhenry 2008-08-02
  • 打赏
  • 举报
回复
还是不行啊,一运行就闪下没了infile>>word就没执行。
j_willl 2008-08-02
  • 打赏
  • 举报
回复
把完整路径写上试试
int main()
{
ofstream outfile("E:\\学习\\程序\\C++程序\\primer\\程序\\DEBUG2\\out_file.txt");
ifstream infile("E:\\学习\\程序\\C++程序\\primer\\程序\\DEBUG2\\infile.txt");
if(!infile)
{
cerr<<"error:unable to open input file!"<<endl;
return -1;
}
if(!outfile)
{
cerr<<"error:unable to open outfile file!"<<endl;
return -1;
}
string word;
while(infile>>word)
outfile<<word<<' ';

return 0;
}
lsm164 2008-08-02
  • 打赏
  • 举报
回复
什么编译器呵,我在g++下没有问题

输入文件的内容:
aaa bbb ccc
ddd
eee


输出文件的内容:
aaa bbb ccc ddd eee


是不是没有创建输入输出文件啊
yangkunhenry 2008-08-02
  • 打赏
  • 举报
回复
不是你们想的那样
infile>>word这句压根儿就没执行啊!
HelloDan 2008-08-02
  • 打赏
  • 举报
回复

#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!"<<endl;
return -1;
}
if(!outfile)
{
cerr<<"error:unable to open outfile file!"<<endl;
return -1;
}
string word;
while(infile>>word)
outfile<<word<<' ';
system("pause");
//or
//getchar();
return 0;
}

tangshuiling 2008-08-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ttkk_2007 的回复:]
最后加上system("pause");
[/Quote]
ttkk_2007 2008-08-02
  • 打赏
  • 举报
回复
最后加上system("pause");
lanne2 2008-08-02
  • 打赏
  • 举报
回复
你在dos下试试

64,683

社区成员

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

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