gpt4 book ai didi

c++ - 如何在 C++ 中跳过读取文件中的一行?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:02 25 4
gpt4 key购买 nike

文件包含以下数据:

#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;
}
}

但由于某种原因它给出了这个错误:

Mycode.cc:13: error: request for member 'find' in 'tableEntry', which is of non-class type 'int'

最佳答案

这更像你想要的吗?

#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;
}

关于c++ - 如何在 C++ 中跳过读取文件中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/576677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com