关于网友提出的“ 每天一问题~fstream 文件打开失败~大神在哪”问题疑问,本网通过在网上对“ 每天一问题~fstream 文件打开失败~大神在哪”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 每天一问题~fstream 文件打开失败~大神在哪描述:
程序木有报错,输入文件名字,然后用fstream 对象打开它;我就在这个程序目录下新建个文件,为什么提示打开失败呢??
#include
#include
#include
#include
using namespace std;
void process(string);
int main(){
vector
string filename,s;
cout<<"enter filenames :(ctrl+z to end)"<<>
while(cin>>filename)
ivec.push_back(filename);
ifstream input;
vector
while(it!=ivec.end()){
input.open(it->c_str());
if(!input){
cerr<<"error can not open file: "
<<*it<<>
input.clear();
it++;
}
else{
while(input>>s)
process(s);
input.close();
input.clear();
it++;
}
system("pause");
return 0;
}
}
void process(string s)
{
cout<<>
}
解决方案1:
抱歉,应该说术语,叫 文件扩展名
就是 这样的 D:/xxx.txt 解决方案2:
参考下面的代码:
#include
#include
#include
#include
using namespace std;
int main(int argc, char** argv)
{
string filename;
vectorfile_vector;
ifstream fis;
cout << "enter file names: (Ctrl + z to quit)" << endl;
while(cin >> filename)
{
file_vector.push_back(filename);
}
for(vector::iterator iter = file_vector.begin(); iter != file_vector.end(); ++iter)
{
fis.open(*iter);
if(!fis)
{
cout << "open file \'" << *iter << "\' failed..." << endl;
}
else
{
cout << "open file \'" << *iter << "\' succeeded..." << endl;
}
fis.close();
}
return 0;
}
假定在E盘的根目录下有以下几个文件存在:
a.ini
abc.txt
out.dat
那么运行上面的程序后,输入:
e:\\a.ini
e:\\abc.txt
e:\\out.dat
^Z
就会输出:
open file 'e:\\a.ini' succeeded...
open file 'e:\\abc.ini' succeeded...
open file 'e:\\out.ini' succeeded...