gpt4 book ai didi

c++ - Boost::iostreams::filtering_istreams 等待 E.O.F

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:41:26 24 4
gpt4 key购买 nike

我将使用 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;
}
}

代码1的输出是

$./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 : .

代码2的输出是

$./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。然后打印输出。

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

最佳答案

您正在查看缓冲。缓冲发生在许多级别。在这种情况下,您可能正在查看过滤流中的缓冲。

你可以玩一些类似的东西

  • 设置设备缓冲区大小
  • 设置过滤器缓冲区大小
  • 设置_pback_buffer_size

在我的机器上,我使用 ( docs) 得到了你想要的结果

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

关于c++ - Boost::iostreams::filtering_istreams 等待 E.O.F,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49366076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com