Boost::iostreams::filtering_istreams 等待 E.O.F

Boost::iostreams::filtering_istreams waiting for E.O.F

提问人:srbcheema1 提问时间:3/19/2018 最后编辑:srbcheema1 更新时间:3/20/2018 访问量:352

问:

我将filtering_istream用作 std::cin 的包装器。 但它没有像我预期的那样工作。 它正在等待 E.O.F 请帮助我理解这种行为。

#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
// compile using g++ -std=c++11 -lboost_iostreams

int main(){
    boost::iostreams::filtering_istream cinn(std::cin);
    std::cout << "Write something:";

    char c;
    while(true){
        cinn.get(c);
        std::cout << "Your character is : " << c << "\n";
        if(c=='.') break;
    }
}

我希望它的工作方式与此代码类似。

#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>

int main(){
    //boost::iostreams::filtering_istream cinn(std::cin);
    std::cout << "Write something:";

    char c;
    while(true){
        std::cin.get(c);
        std::cout << "Your character is : " << c << "\n";
        if(c=='.') break;
    }
}

code1 的输出是

$./a.out
hello
how.are_you
Write something:Your character is : h
Your character is : e
Your character is : l
Your character is : l
Your character is : o
Your character is : 

Your character is : h
Your character is : o
Your character is : w
Your character is : .

Code2 的输出是

$./a.out
Write something:hello
Your character is : h
Your character is : e
Your character is : l
Your character is : l
Your character is : o
Your character is : 

how.are_you
Your character is : h
Your character is : o
Your character is : w
Your character is : .

代码 2 给出了我预期的输出。它读取每一行并对其进行处理。而 Code1 读取所有行,直到它获得 E.O.F.,然后打印输出。

这两个代码的行为不同。我无法理解这种行为。请帮忙。提前致谢。

C++ IOSTREAM 提升-IOstreams

评论


答:

0赞 sehe 3/20/2018 #1

您正在研究缓冲。缓冲发生在许多级别。在这种情况下,您可能会查看筛选流内部的缓冲。

你可以玩一些东西,比如

  • set_device_buffer_size
  • set_filter_buffer_size
  • set_pback_buffer_size

在我的机器上,我得到了你想要的结果(文档)

boost::iostreams::filtering_istream cinn(std::cin, 0, 1);