读书人

fstream读入文件成功与否的有关问题

发布时间: 2013-10-21 17:02:52 作者: rapoo

fstream读入文件成功与否的问题

谢谢你,上半年我还是刚入门,我不知道为什么你很少关注别人的空间,然后关注了我,让我受宠若惊
[解决办法]


[解决办法]
引用:
Quote: 引用:

Quote: 引用:

编译成功并且运行后,为什么第一个if语句不执行?

注:
1、程序所在的文件夹里有名为“Saiyan_input”和"Saiyan_output"的这两个.txt文件。
2、编译软件是VS 2008

//
#include <iostream>
#include <fstream>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::cerr;

void keep_window_open();

int main()
{
// construct an ifstream and bind it to the file named Saiyan_input
ifstream infile("Saiyan_input.txt");
// ofstream output file object to write file named Saiyan_input
ofstream outfile;
outfile.open("Saiyan_output.txt");

// check that the open succeeded
if(infile)
{
cout<<"\nGood! We can open input file: "
<<"Saiyan_input"<<endl;

}
if(outfile)
{
cout<<"\nPerfect! We can open output file: "
<<"Saiyan_output"<<endl;
}

keep_window_open();
}

void keep_window_open()
{
cout << "\nPress any key to exit:";


getchar();
}




楼主,你这个问题其实很容易解决;
if(infile)
如果打开了文件就返回1;但是没执行就是返回了0了?在这个题目上可以这样理解;
所以说,你的这个 txt文件是不存在的,如此我们可以建立这么一个文件
在 第15行加上:ofstream file("Saiyan_input.txt"); 即可。fstream读入文件成功与否的有关问题


文件确实存在,这个是我检查了半个小时的结论;况且第二个if完全可以执行。


你尝试下我的提供的方法,能成功的。 可能是路径问题把

读书人网 >C++

热点推荐