c++从文件中读取,跳过一些信息

C++ read from file, skipping some information

本文关键字:信息 文件 读取 c++      更新时间:2023-10-16

我需要一种读取以下文件的方法:

This is a line but not needed
This is another line not needed
name   telephone
john   0000001
mark   0000002
.
.
.

我想跳过第一行,第三行是标题。我想把名字保存在一个字符串变量中,电话也保存在一个字符串变量中。数据之间有制表符

所以这是我的代码,但不能工作。

string line; 
while (getline(infile, line)) 
{
  istringstream iss(line);
  int a, b; 
  if (!(iss >> a >> b)) 
  { break; } // error 
  // process pair (a,b) 
}

所以我想知道如何跳过一些信息,如标题和解析变量中的信息。欢呼。

如果您继续从文件中读取,但只有在读取电话后才开始存储到字符串中呢?

像这样:

string str, newStr;
boolean = false;
while(cin >> str){
    if(str.compare("telephone")== 0)
        boolean = true;
    if(boolean)
        newStr += str;
}
return newStr;

确保为比较方法包含字符串