使用 istream::peek() 将下一个值与当前值进行比较

Using istream::peek() to compare next value against current value

为什么我的 peek() 条件在下一个值为 50 时不返回 FALSE?

当我的代码从一个文件中读入 40 并比较 50 是否小于 40 时,它 returns TRUE.This 显然是错误的并且在我的子程序中的数字顺序中产生了一个错误文件出现。

while (fin_dataFile >> value) //fin_dataFile is type ifstream reading the original data
    {
        // write the int stored in value to the second sub file
        // fout_splitFile2 is type ofstream
        fout_splitFile2 << value << " ";

        // if the next value in the original data set is less than the current value
        // that was read, break the loop so we can read in that next value to store it
        // in the first sub file instead 
        if (fin_dataFile.peek() < value)
        {
            break;
        }
    }

我输入了一个 temp int 来告诉我 fin_dataFile.peek() 返回了什么,我一直得到 32。这是 space 的 ascii 值。有道理,因为每个数字都是 space 分隔的。我尝试使用以下方法解决此问题:

if ((fin_dataFile >> std::ws).peek() < value)

但是 peek() 仍然返回与文本文件中不同的数字。

背景:

我正在研究外部自然归并排序。我的拆分函数没有正确拆分数据。它应该读入一个 space 分隔数字的文件,并将它们分成两个子排序数据的子文件。 确实如此,但顺序不正确。

我的算法通过使用 peek() 比较主文件中的下一个数字与已读入的当前数字来划分文件之间的数字。

这是要拆分的数据的示例文本文件:

75 55 15 20 85 30 35 10 60 40 50 25 45 80 70 65

peek() 向前浏览文件中的下一个 字符

在您的例子中,文件中“40”之后的下一个字符是 space 字符,ASCII space,或小于 40 的 32。