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

fstream读入文件成功与否的问题
本帖最后由 u011873969 于 2013-10-10 02:08:15 编辑
编译成功并且运行后,为什么第一个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();
}
fstream

------解决方案--------------------
可能和当前路径有关: 
在vc/vs中F5(调试运行)的当前路径是源码所在路径,Ctrl+F5(直接运行)的当前路径是exe所在路径.


------解决方案--------------------
引用:
先检查文件名是否拼写错误。


图灵狗大哥好~!fstream读入文件成功与否的有关问题
谢谢你,上半年我还是刚入门,我不知道为什么你很少关注别人的空间,然后关注了我,让我受宠若惊
------解决方案--------------------
引用:
编译成功并且运行后,为什么第一个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读入文件成功与否的有关问题
------解决方案--------------------
引用:
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:";