如何重新启用由另一端关闭的 istream FIFO

How to re-enable an istream FIFO closed by the other end?

本文关键字:istream FIFO 何重新 启用      更新时间:2023-10-16

我正在使用istream通过FIFO接收数据。一旦另一端关闭它,尽管我清除了eof位,但我无法再次做好准备。badbit总是在那里。

如何在阅读模式下重新启用我的先进先出?

这是我的代码片段:

std::istream &os;
// ...
try {
    os.get(tmp, length+1);
}
catch(std::exception const& e)
{
    std::cerr << "exception failure  caught: " << e.what() << 'n';
}
if ((os.rdstate() & std::ios_base::eofbit) > 0) {
    os.clear();
    os.seekg(0, std::ios::beg);
}

解决方案最终是将 std::ifstream 替换为 std::filebuf,并在 istream 不再可用时关闭 filebuf

std::filebuf fb;
while (1==1)
    {
    if fb.open(myfifo, std::ios::binary|std::ios::in)
    {
    std::istream infifo(&fb);
    while (infifo)
    {
        infifo >> *tmp;
        // do your job
    }
    fb.close();
    }