为啥fstream 对象打开文件失败?新手求教!
#include<iostream>
#include<fstream>
#include<string>
//#include<vector>
using namespace std;
int main()
{
fstream opName;
string str;
cout<<"Input file's name:"<<endl;
cin>>str;
opName.clear();
opName.open(str.c_str());
//cout<<"111"<<endl;//test statemate;
if(!opName)
{
cerr<<"Open "<<str<<" fail!"<<endl;
return -1;
}
string s;
while(opName>>s)
{
cout<<s<<' ';
}
opName.close();
return 0;
}
已经创建文件放到工程目录下面,但还是打开失败,不知道为什么!