我在桌面新建的文件 编译后打开新建的文件名称 显示打开失败 请大佬们指点是什么原因呢
//打开给定的文件并读取其内容#include<iostream>
#include<string>
#include<stdexcept>
#include<fstream>
using namespace std;
istream &f(istream & in)
{
string ival;
while(in>>ival,!in.eof())
{
if(in.bad())
throw runtime_error("IO stream corrupted");
if(in.fail())
{
cerr<<"bad data,try again:";
in.clear();
in.setstate(istream::eofbit);
continue;
}
cout<<ival<<endl;
}
in.clear();
return in;
}
int openFile(string fName)
{
fstream fOpen;
fOpen.clear();
fOpen.open(fName.c_str());
if(!fOpen)
{
cerr<<"打开文件失败"<<endl;
return -1;
}
string s;
while(fOpen>>s)
{
f(fOpen);
cout<<s<<" ";
}
fOpen.close();
return 1;
}
int main(void)
{
cout<<"请输入你要打开的文件名:"<<endl;
string fName;
cin>>fName;
openFile(fName);
system("pause");
return 0;
}