2

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

当我的代码从文件中读取 40 并比较 50 是否小于 40 时,它返回 TRUE。这显然是错误的,并按照我的子文件中的数字出现的顺序创建了一个错误。

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。这是空格的 ascii 值。有意义,因为每个数字都是空格分隔的。我尝试使用以下方法解决此问题:

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

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

背景:

我正在研究外部自然归并排序。我的拆分功能没有正确拆分数据。它应该读入一个空格分隔的数字文件,并将它们分成两个子文件的子排序数据。它这样做了,但顺序不正确。

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

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

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

1 回答 1

4

peek()提前查看文件中的下一个字符

在您的情况下,文件中“40”之后的下一个字符是空格字符、ASCII 空格或 32,小于 40。

于 2015-07-02T01:18:55.150 回答