如何开始读取文件中文本的某些部分,一旦找到文本之前的关键字

How to start reading some part of the text in a file once a keyword preceeding the text is found

本文关键字:文本 关键字 些部 开始 读取 文件 何开始 中文      更新时间:2023-10-16

这是我写的代码…非常基本,因为我是初学者.....

源文件如下:

Integers:
1 2 3 4 56 ...
String:
This is a string......
...(text).....

代码应该根据开头遇到的关键字读取文本…

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    int i;
    string STRING;
    char *inname = "source.txt";
    ifstream infile(inname);
    if (!infile) 
    {
        cout << "There was a problem opening file "<< inname<< " for reading."<< endl;
        return 0;
    };
    while (STRING != "Integer:")
        {
            getline(infile,STRING); // Saves the line in STRING.
            cout<<STRING<<endl; // Prints our STRING.
        };
    };
    cout << "Opened " << inname << " for reading." << endl<<endl<<"Integers:";
    while (infile >> i) {
        cout<<endl<<i<<endl;
    infile.close();
    return 0;
    }
}

非常感谢!

您需要对该行中找到的单词进行标记。有很多方法可以做到这一点,您可以使用stringstream, boost tokenizer,甚至编写自己的代码。

假设您创建了一个规则,其中每个单词(标记)由空格分隔,并且您已经设法对行进行了标记,那么您可以编写代码检查该行中的第一个标记并适当地

恕我冒昧,您的解析器中缺少一些状态,一些检查文件是否仍然有效并解析整数。

这是一个概念证明:

#include <fstream>
#include <iostream>
#include <vector>
namespace {
  void parse_integers(std::vector<int> & vi, std::string const & line) {
    // Using strtol here - other tokenizers are possible.
    char const * ip { line.c_str() };
    char * ep;
    do {
      long int const v { strtol( ip, &ep, 10 ) };
      vi.push_back( v );
      ip = ep;
    } while( *ep != '' );
  }
}
int main() {
  std::string   inname { "source.txt" };
  std::ifstream infile { inname };
  if( ! infile ) {
    std::cout << "There was a problem opening file "
          << inname << " for reading." << std::endl;
        return 0;
  };
  enum class ParseState {
    outer, found_integers, found_string
  };
  ParseState ps { ParseState::outer };
  std::vector<int> s_integers;
  std::string      s_string;
  while( infile ) {
    std::string line;
    getline( infile, line );
    // Skip empty lines
    if( line.size() == 0 ) {
      continue;
    }
    if( line == "Integers:" ) {
      ps = ParseState::found_integers;
      continue;
    } else if( line == "String:" ) {
      ps = ParseState::found_string;
      continue;
    }
    // Hope that a section was already found....
    if( ps == ParseState::outer ) {
      std::cerr << "Line with data but without a section found ["
        << line << "]" << std::endl;
      continue;
    }
    switch( ps ) {
    case ParseState::found_integers:
      parse_integers(s_integers, line);
      break;
    case ParseState::found_string:
      s_string += line + "n";
      break;
    case ParseState::outer:
      // Cannot happen
      abort();
    }
  }
  std::cout << "Dump file contents" << std::endl;
  std::cout << "Strings: [" << s_string << "]" << std::endl;
  std::cout << "Integers: ";
  for(int i : s_integers) {
    std::cout << "[" << i << "] ";
  }
  std::cout << std::endl;
  return 0;
}
输入:

Integers:
1 2 3 4 56 111 761 777
String:
This is a string......
...(text).....
输出:

Dump file contents
Strings: [This is a string......
...(text).....
]
Integers: [1] [2] [3] [4] [56] [111] [761] [777]