求大神解决啊,C++程序里用ignore为什么读出来是空的
这是程序:#include<iostream>#include<fstream>#include<string>usingnamespacestd;intmain(){s...
这是程序:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string fileName,a,b,c;
ifstream inFile;
cout<<"please input the fileName"<<endl;
cin>>fileName;
inFile.open(fileName.c_str());
inFile.ignore(200,',');
inFile>>a;
inFile.ignore(200,',');
inFile>>b;
inFile.ignore(200,',');
inFile>>c;
cout<<a<<endl<<b<<endl<<c<<endl;
inFile.close();
return 0;
}
我要读取一个文本文档中的东西:the,dog,disk,boom.break,build。要忽略逗号以及逗号前的东西 展开
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string fileName,a,b,c;
ifstream inFile;
cout<<"please input the fileName"<<endl;
cin>>fileName;
inFile.open(fileName.c_str());
inFile.ignore(200,',');
inFile>>a;
inFile.ignore(200,',');
inFile>>b;
inFile.ignore(200,',');
inFile>>c;
cout<<a<<endl<<b<<endl<<c<<endl;
inFile.close();
return 0;
}
我要读取一个文本文档中的东西:the,dog,disk,boom.break,build。要忽略逗号以及逗号前的东西 展开
1个回答
展开全部
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string fileName,a,b,c;
ifstream inFile;
cout<<"please input the fileName"<<endl;
cin>>fileName;
inFile.open(fileName.c_str());
// if(not inFile.is_open()){
// std::cerr<<"unable to open the file";
// while(1);
// return 0;
// }
inFile.ignore(200,',');
inFile>>a;
inFile.ignore(200,',');
inFile>>b;
inFile.ignore(200,',');
inFile>>c;
cout<<a<<endl<<b<<endl<<c<<endl;
inFile.close();
return 0;
}
更多追问追答
追问
不行啊,输出还是空的。。
追答
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string fileName,a,b,c;
ifstream inFile;
cout<<"please input the fileName"<<endl;
cin>>fileName;
inFile.open(fileName.c_str());
if(not inFile.is_open()){
std::cerr<<"unable to open the file";
while(1);
return 0;
}else{
std::cerr<<"the file is successfully opened";
}
inFile.close();
return 0;
}
看输出是什么
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询