C++ 如何跳过在C++;?
该文件包含以下数据:C++ 如何跳过在C++;?,c++,file-io,C++,File Io,该文件包含以下数据: #10000000 AAA 22.145 21.676 21.588 10 TTT 22.145 21.676 21.588 1 ACC 22.145 21.676 21.588 我尝试使用以下代码跳过以“#”开头的行: #include <iostream> #include <sstream> #include <fstream> #include <string> using namespace
#10000000 AAA 22.145 21.676 21.588
10 TTT 22.145 21.676 21.588
1 ACC 22.145 21.676 21.588
我尝试使用以下代码跳过以“#”开头的行:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main() {
while( getline("myfile.txt", qlline)) {
stringstream sq(qlline);
int tableEntry;
sq >> tableEntry;
if (tableEntry.find("#") != tableEntry.npos) {
continue;
}
int data = tableEntry;
}
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
while(getline(“myfile.txt”,qlline)){
stringstream sq(QLINE);
int表格条目;
sq>>表格条目;
if(tableEntry.find(“#”)=tableEntry.npos){
继续;
}
int data=表格条目;
}
}
但出于某种原因,它会产生以下错误:
Mycode.cc:13:错误:请求
“tableEntry”中的成员“find”,其中
属于非类类型“int”
您尝试从行中提取一个整数,然后尝试在整数中找到一个“#”。这没有意义,编译器抱怨没有整数的
find
方法
您可能应该直接在循环开头的读取行上检查“#”。
除此之外,您还需要声明qlline
,并实际在某个地方打开文件,而不仅仅是将带有名称的字符串传递给getline
。基本上是这样的:
ifstream myfile("myfile.txt");
string qlline;
while (getline(myfile, qlline)) {
if (qlline.find("#") == 0) {
continue;
}
...
}
这更像你想要的吗
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
fstream fin("myfile.txt");
string line;
while(getline(fin, line))
{
//the following line trims white space from the beginning of the string
line.erase(line.begin(), find_if(line.begin(), line.end(), not1(ptr_fun<int, int>(isspace))));
if(line[0] == '#') continue;
int data;
stringstream(line) >> data;
cout << "Data: " << data << endl;
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
fstream-fin(“myfile.txt”);
弦线;
while(getline(fin,line))
{
//下一行从字符串开始修剪空白
line.erase(line.begin(),find_if(line.begin(),line.end(),not1(ptr_fun(isspace)));
如果(第[0]行=“#”)继续;
int数据;
stringstream(line)>>数据;
我也可以建议在检查注释字符之前修剪行。你是对的,我已经编辑了示例以修剪字符串的开头。+1用于编译器。错误的哪一部分你不明白?xtofl:哥们,如果我可以+1注释,我会笑得屁滚尿流:)