c++ - 使用指定的分隔符逐行解析文本文件

标签 c++ parsing boost text-files

我需要使用 boost 从文本文件的每一行中获取数字:

d$+$B$ 0.0   000 000 000    1.0   255 255 255

类似的东西:

0.0   000 000 000    1.0   255 255 255

在 Qt 中,它是这样的:

 QString data = input->readLine();
 o_name = data.section('$', 0, 2);
 QStringList dataLst = data.section('$', 3, 3).split(' ', QString::SkipEmptyParts);
while(!dataLst.isEmpty() )
//process

boost 中的等效算法是什么?

到目前为止,我只有这个:

boost::iostreams::basic_file<float>( filename,  std::ios_base::in  );

最佳答案

这是一个使用 boost 正则表达式的简短代码片段。我试图使用 c++11 进行相同的工作,但似乎 gcc 4.8.2 还不支持它:GCC GNU

  std::ifstream ifStreamObject("test", std::ifstream::in);
  if (!ifStreamObject.is_open())
  {
    // nothing to do
    return -1;
  }
  std::string line;
  boost::regex e("\\s+"); // split on whitespaces
  boost::sregex_token_iterator j;
  while (std::getline(ifStreamObject, line))  
  {
    boost::sregex_token_iterator i(line.begin(), line.end(), e, -1);

    while(i!=j)
    {
      // display the tokens for now.. process the data here
      cout << *i++ << endl;
    }
  }

输出:

d$+$B$
0.0
000
000
000
1.0
255
255
255

关于c++ - 使用指定的分隔符逐行解析文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21608587/

相关文章:

c++ - 永远不会计算为常量表达式的 lambda() 可以是 C++ 中的 `constexpr` 函数吗?

html - 在 Dart 中将 HTML 字符串解析为 DOM

php - 使用 PHP 简单 HTML DOM 解析器时遇到错误

c++ - boost xml_parser 以格式化空标签

c++ - '.' token c++ 之前的预期初始化器

c++ - Visual Studio : how to start up a project without the console window poping up

python - 一步一步让 NLTK 中的麦芽解析器正常工作?

c++ - boost json_parser 警告 C4715

c++ - 如何使用boost C++解析8位十六进制数和字符串?

c++ - Boost C++ 是否有下载/网络库