我需要使用 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/