运行结果老是打不开文件
函数打开文件用于输入,将文件内容读入string类型的vector容器中,每一行存储为该容器对象的一个元素#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int fileToVector(string fileName,vector<string>& svec)
{
ifstream inFile(fileName.c_str()); //创建ifstream对象inFile并绑定到由形参fileName指定的文件
if(!inFile)
return 1;
string s;
while(getline(inFile,s))
svec.push_back(s);
inFile.close();
if(inFile.eof())
return 4;
if(inFile.bad())
return 2;
if(inFile.fail())
return 3;
}
int main()
{
vector<string>svec;
string fileName,s;
cout<<"enter filename:"<<endl;
cin>>fileName;
switch(fileToVector(fileName,svec))
{
case 1:
cout<<"error:can not open file:"
<<fileName<<endl;
return -1;
case 2:
cout<<"system failure"<<endl;
return-1;
case 3:cout<<"read failure"<<endl;
return -1;
}
cout<<"vector:"
<<endl;
for(vector<string>::iterator iter=svec.begin();iter!=svec.end();++iter)
cout<<*iter<<endl;
return 0;
}
我在桌面建了一个文件,可是我把文件输进去了(有扩展名没扩展名都输过了,有的还输了路径),可是结果老师can not open file:.....
到底要怎么弄才能够打呢?