提取c ++输出中某个单词前面的值

时间:2013-07-24 08:06:25

标签: c++ string output ifstream

如果您的文件具有已知结构的文本,您将如何提取某个标识字之前的值?具体来说,如何从下面的文本中提取值。

CDM-nucleon micrOMEGAs amplitudes:
proton:  SI  -3.443E-10 

以下是我对剧本的了解:

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
string identifier;
double value;
ifstream file("output.txt");
// Commands to extract value
file.close();

return 0;
}

非常感谢。

1 个答案:

答案 0 :(得分:0)

这取决于输出文件的复杂性和大小。 “复杂性”我的意思是说,有可能找到你的识别词,而不是你想要解析的数量和种类的关键词。 对于大文件,将其加载到内存中可能是不合理的,因此可能是流式传输。

在您的特定情况下,您可能需要查看此内容:

http://www.cplusplus.com/reference/regex/

或其中一个适用于流数据的解析器:

http://en.wikipedia.org/wiki/Flex_lexical_analyser

您也可以查看其他语言,例如perl来解析它。