【问题标题】:istream_iterator try to parse invalid dataistream_iterator 尝试解析无效数据
【发布时间】:2011-10-29 05:28:10
【问题描述】:

您好,我希望有人能帮助理解以下代码的这种行为。

#include <iostream>
#include <algorithm>
#include <string>
#include <limits>
#include <fstream>
#include <iterator>
#include <stdexcept>


struct asound_stanza
{
    unsigned index;
    std::string name;

    friend std::istream &operator>>(std::istream &is, asound_stanza &stan)
    {
        is >> stan.index;
        if(!is.good())
            return is;
        std::getline(is, stan.name);
        std::string::const_iterator
            eol   = stan.name.cend(),
            start = std::find(stan.name.cbegin(), eol, '['),
            end   = std::find(start, eol, ' ');
        stan.name = std::string(++start, end);
        is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        // std::istream_iterator<char> it;
        // while(*it++!=0x0a);
        return is;
    }
};

int main()
{
    std::ifstream is("/proc/asound/cards", std::ifstream::in); 
    std::istream_iterator<asound_stanza> it(is), end;
    unsigned devid = 0;
    std::for_each(it, end, [&](const asound_stanza &stan)->void{
        if(stan.name!="CODEC")
            return;
        devid = stan.index;
    });
    std::cout << devid;
    return 0;
}

这可行,但我有几个问题。在完成所有有效的迭代之后,它开始尝试解析另一个不可避免地失败的迭代(因此是if(!is.good())..._)。它试图解析它,但它永远不会将最终格式错误的结构传递给 lambda 表达式。为什么不呢?是不是因为streams不好()所以懒得尝试传入?

另外,我怎样才能让它甚至不费心尝试解析最终结构,(每个节都以换行符(0x0a)结尾,所以我会认为忽略流直到换行符会导致 EOF 在最终有效的迭代,但它没有。

感谢您的指导。

还可以随意传递另一个编码正确性 cmets。

PS:我的文件是这样的

0 [Intel          ]: HDA-Intel - HDA Intel
                     HDA Intel at 0xfc500000 irq 46
1 [HDMI           ]: HDA-Intel - HDA ATI HDMI
                     HDA ATI HDMI at 0xfc010000 irq 47
2 [CODEC          ]: USB-Audio - USB Audio CODEC
                     Burr-Brown from TI USB Audio CODEC at usb-0000:00:1d.7-3.1, full speed

【问题讨论】:

    标签: c++ stream c++11 iterator stl-algorithm


    【解决方案1】:

    您的代码应如下所示:

    friend std::istream &operator>>(std::istream &is, asound_stanza &stan)
    {
        if ( !(is >> stan.index) )
            return is;  //return if couldn't read unsigned int
    
        if(std::getline(is, stan.name))//go inside ONLY IF read is successful
        {
           std::string::const_iterator 
           eol  =stan.name.cend(),
           start=std::find(stan.name.cbegin(), eol, '['),
           end  =std::find(start, eol, ' ');
           stan.name=std::string(++start, end);    
           is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        return is;
    }
    

    阅读上面我修改中的cmets!

    因为这个已经讨论过很多次了,我之前也回答过很多次了,所以我不想再重复了。请阅读我的另一个答案,该答案解释了这一点:

    【讨论】:

    • 谢谢,这是一个更好的方法,但我想知道为什么它没有将格式错误的节传递给 lambda 以及为什么它甚至试图解析最后一个忽略肯定应该设置EOF?
    • @111111:再次查看我的答案。我之前已经解释过,所以请阅读我的其他答案。
    • 以上说的还不错。但就我个人而言,我会将这两个读取放在一个 if 中。 if ((is &gt;&gt; stan.index) &amp;&amp; (std::getline(is, stan.name)) { /* We got the object */}
    • @Martin 现在就是这样
    【解决方案2】:

    如果流处于无效状态或尝试从操作符内的流读取>>失败并将流设置为无效状态,istream_iterator 将自身设置为流位置的末尾。因此迭代结束,甚至没有查看部分解析的对象。

    【讨论】:

    • 感谢您回答了我的一个问题,即为什么它会显示它的行为,但是我很想知道为什么 ignore(...max(), '\n');没有设置 EOF 标志。
    • @111111:忽略读取 upto EOF(但不是过去)。所以它可以从流中读取所有字符。它总是第一个尝试读取文件末尾的操作失败(或设置 EOF 标志)。注意:最后一次好的操作将读取到 eof。
    猜你喜欢
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多