首页 > 解决方案 > 从行读取时清除 Ifstream 文件

问题描述

我正在尝试从 C++ 中的输入纺织品中读取由空格分隔的字符。我已成功打开该文件并能够遍历它。但是,每当我尝试从文件中的字符中分配变量时,这些行似乎都被清除了。第一行的变量设置正确,但不在第一行之后。我不确定我是否错误地访问了 infile,是否缺少标志,或者是否有更好的方法来访问字符。

代码:

int lineNumber = 0,*linesMaxVal = new int[lineCount];
string line;
while (getline(infile, line)) {

    cout << "line # is " << lineNumber << " line val is: " << "'" << line << "'" << endl;

    int x1,x2,x3,x4,x5,max;
    infile >> x1 >> x2 >> x3 >> x4 >> x5;
}

如果去掉“infile >> x1 >> x2 >> x3 ... etc”行,输出如下(正确)

line # is 0 line val is: '0 0 0 0 0'
line # is 0 line val is: '1 2 3 4 5'
line # is 0 line val is: '5 4 3 2 1'
line # is 0 line val is: '5 100 1000 10000 10500'
line # is 0 line val is: '99999 99999 99998 99999 99999'
line # is 0 line val is: '-1 0 0 0 -1'
line # is 0 line val is: '-9999 -9999 -9998 -9999 -9999'
line # is 0 line val is: '50 1234 457 789 7'
line # is 0 line val is: '123240 -124 -453 234 -99999'
line # is 0 line val is: '234 235 -109 0 12'
line # is 0 line val is: '777 987 234 345 111'
line # is 0 line val is: '12 0 -19 27 19'
line # is 0 line val is: '0 0 0 0 0'
line # is 0 line val is: '-99 -900 45 20 10'
line # is 0 line val is: '13 -56 99 123 90'
line # is 0 line val is: '0 0 1 -11 -1'
line # is 0 line val is: ''

否则,如果保留infile行并定义变量,则输出如下。

line # is 0 line val is: '0 0 0 0 0'
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''
line # is 0 line val is: ''

任何帮助表示赞赏!

标签: c++ifstream

解决方案


infile >> x1 >> x2 >> x3 >> x4 >> x5;

Reads all of the numbers on the line but leaves the newline character in the stream.

Then you go back to the head of your loop and run:

getline(infile, line)

Which immediately sees a newline, so it gives you an empty string, eats the newline and moves on.

The big issue here is that the extraction operator (>>) leaves the whitespace it stops at in the stream (in this case a newline), while getline eats whatever delimeter it stops on. Mixing the two can cause little hiccups like this.

One possible fix could be to add an ignore after your extraction operators to get the newline out of the stream.


After some talk in chat, it looks like you want to loop through the file and find the largest value on each line. I'd suggest using a vector to store each line's values and using the algorithm library to find the max:

//Our temp vector. It'll hold a line worth of ints
vector<int> line(5);

while(true) { //one loop = one line

    //Loop and read 5 ints (one line's worth) into the vector 
    for(int x = 0; x < 5 && infile >> line.at(x); x++) { }

    if(!infile) { break; } //if we read through everything, we're done!

    //find the biggest value in the vector
    int biggest = *max_element(line.begin(), line.end());

    //Do some things with it...
    cout << "Biggest is: " << biggest << endl;
}

For a working demo, see here: ideone


推荐阅读