【发布时间】:2020-02-21 03:54:45
【问题描述】:
我正在尝试逐行读取文件。我的文件有点像这样:
4 558 5
123 145 782
x 47 45 789
如果第一个字符是 a,我想将它前面的三个值存储在一个数组中。我正在尝试这个,但它似乎不起作用:
while (std::getline(newfile, line))
{
if (line[0] == 'a')
{
vertex[0] = line[1];
vertex[1] = line[2];
vertex[2] = line[3];
//vertices.push_back(vertex);
}
【问题讨论】:
-
请提供minimal reproducible example。
line[1]是该行 ' ' 中的第二个字符。这不是第一个数字。 -
if (line.empty()) continue;作为循环体的第一行可能更安全一些。