繁体   English   中英

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

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

当下一个值为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;
        }
    }

我输入了一个临时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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM