每天一有关问题~fstream 文件打开失败~大神在哪

每天一问题~fstream 文件打开失败~大神在哪程序木有报错,输入文件名字,然后用fstream 对象打开它;我就在这

每天一问题~fstream 文件打开失败~大神在哪
程序木有报错,输入文件名字,然后用fstream 对象打开它;我就在这个程序目录下新建个文件,为什么提示打开失败呢??


#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
void process(string);
int main(){
  vector<string> ivec;
string filename,s;
cout<<"enter filenames :(ctrl+z to end)"<<endl;
while(cin>>filename)
ivec.push_back(filename);
ifstream input;
vector<string>::const_iterator it= ivec.begin();
while(it!=ivec.end()){
input.open(it->c_str());
if(!input){
cerr<<"error can not open file: "
<<*it<<endl;
input.clear();
it++;
}
else{
while(input>>s)
process(s);
input.close();
input.clear();
it++;
}
system("pause");
  return 0;
}
}
void process(string s)
{
cout<<s;
}

[解决办法]

C/C++ code
#include<iostream>#include<string>#include<vector>#include<fstream>using namespace std;void process(string);int main(){    vector<string> ivec;    string filename,s;    cout<<"enter filenames :(ctrl+z to end)"<<endl;    while(cin>>filename)        ivec.push_back(filename);    ifstream input;    vector<string>::const_iterator it= ivec.begin();    while(it!=ivec.end()){        input.open(it->c_str());        if(!input){            cerr<<"error can not open file: "                <<*it<<endl;            input.clear();            it++;        }        else{            while(input>>s)                process(s);            input.close();            input.clear();            it++;        }        system("pause");        return 0;    }}void process(string s){    cout<<s;}
[解决办法]
ifstream 不能用来创建文件,如果文件不存在就会打开失败
用ofstream 或则 fstream
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
void process(string);
int main(){
vector<string> ivec;
string filename,s;
cout<<"enter filenames :(ctrl+z to end)"<<endl;
while(cin>>filename)
ivec.push_back(filename);
fstream input;//ifstream ---> fstream
vector<string>::iterator it= ivec.begin();
while(it!=ivec.end()){
input.open(it->c_str(),ios::out);//这里
if(!input){
cerr<<"error can not open file: "
<<*it<<endl;
input.clear();
it++;
}
else{
while(input>>s)//这里是不是 <<
process(s);
input.close();
input.clear();
it++;
}
system("pause");
return 0;
}
}
void process(string s)
{
cout<<s;
}