缓冲流和非缓冲流 [英] Buffered and unbuffered stream

查看:315
本文介绍了缓冲流和非缓冲流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有缓冲流的情况下,它在书中说,它等待直到缓冲区已满再写回监视器。例如:

In case of buffered stream it said in a book that it wait until the buffer is full to write back to the monitor. For example:

cout << "hi"; 




  1. 缓冲区已满是什么意思。

  1. What do they mean by "the buffer is full".

cerr << "hi";


  • 在我的书中说,所有发送到 cerr 立即写入标准错误设备,这是什么意思?

  • It is said in my book that everything sent to cerr is written to the standard error device immediately, what does it mean?

    char *ch;
    cin>> ch; // I typed "hello world";
    


  • 在此示例中, ch 将被分配给 hello和 world将被忽略,是否意味着它仍然在缓冲区中,并且会影响将来的语句结果?

  • In this example ch will be assigned to "hello" and "world" will be ignored does it mean that it still in the buffer and it will affect the results of future statements?


    推荐答案

    您的书似乎不太有用。

    1)输出流将其字节发送到 std :: streambuf ,其中
    可能包含缓冲区;
    std使用的 std :: filebuf (源自 streambuf ) :: ofstream 通常会被缓冲。这意味着当您输出
    时,它不一定立即输出;它会将
    写入缓冲区,并且仅在
    缓冲区已满或您以某种方式显式请求时才输出到OS,通常是通过调用
    在流上使用flush()(直接或间接使用 std :: endl )。
    但是,这可能有所不同;输出到 std :: cout
    stdout 同步,大多数实现或多或少都遵循
    stdout 的规则,用于 std :: cout ,如果输出
    ,则更改缓冲策略要去互动设备。

    1) The output streams send their bytes to a std::streambuf, which may contain a buffer; the std::filebuf (derived from streambuf) used by and std::ofstream will generally be buffered. That means that when you output a character, it isn't necessarily output immediately; it will be written to a buffer, and output to the OS only when the buffer is full, or you explicitly request it in some way, generally by calling flush() on the stream (directly, or indirectly, by using std::endl). This can vary, however; output to std::cout is synchronized with stdout, and most implementations will more or less follow the rules of stdout for std::cout, changing the buffering strategy if the output is going to an interactive device.

    无论如何,如果不确定,并且您想确保输出
    确实离开了程序,只需添加对flush的调用。

    At any rate, if you're unsure, and you want to be sure that the output really does leave your program, just add a call to flush.

    2)您的书在这里错了。

    2) Your book is wrong here.

    其中一种缓冲策略是 unitbuf ;这是
    std :: ostream 中的标志,您可以设置或重置它( std :: ios_base :: set()
    std :: ios_base :: unset() std :: ios_base
    std :: ostream 的基类,因此您可以在 std :: ostream
    对象)。设置 unitbuf 后, std :: ostream 将调用添加到 flush()
    到每个输出函数的末尾,因此当您编写:

    One of the buffering strategies is unitbuf; this is a flag in the std::ostream which you can set or reset (std::ios_base::set() and std::ios_base::unset()std::ios_base is a base class of std::ostream, so you can call these functions on an std::ostream object). When unitbuf is set, std::ostream adds a call to flush() to the end of every output function, so when you write:

    std::cerr << "hello, world";
    

    将在字符串$ all 中的所有字符后刷新流如果设置了 unitbuf ,则会输出b $ b。在启动时,将 unitbuf 设置为 std :: cerr
    ;默认情况下,未在任何其他文件上设置它。但是您
    可以随意设置或取消设置它。我建议反对
    std :: cerr 上取消设置,但是如果输出 std :: cout

    the stream will be flushed after all of the characters in the string are output, provided unitbuf is set. On start-up, unitbuf is set for std::cerr; by default, it is not set on any other file. But you are free to set or unset it as you wish. I would recommend against unsetting it on std::cerr, but if std::cout is outputting to an interactive device, it makes a lot of sense to set it there.

    请注意,这里所要讨论的只是<$中的缓冲区。 c $ c> streambuf
    通常,操作系统还会缓冲。刷新缓冲区所做的全部工作是
    将字符传输到操作系统;这意味着需要事务完整性时,不能直接使用
    ofstream

    Note that all that is in question here is the buffer in the streambuf. Typically, the OS also buffers. All flushing the buffer does is transfer the characters to the OS; this fact means that you cannot use ofstream directly when transactional integrity is required.

    3)当您使用>> 输入字符串或字符缓冲区,则
    std :: istream 首先跳过前导空白,然后输入最多
    ,但不包括下一个空白。在
    标准的正式术语中,它从流中提取字符,因此不会再次显示它们
    (除非您寻求,如果流支持它)。
    下一个输入将从上一个中断的地方开始。不管下面的字符
    是在缓冲区中还是仍在磁盘上,都与
    无关。

    3) When you input to a string or a character buffer using >>, the std::istream first skips leading white space, and then inputs up to but not including the next white space. In the formal terms of the standard, it "extracts" the characters from the stream, so that they will not be seen again (unless you seek, if the stream supports it). The next input will pickup where ever the previous left off. Whether the following characters are in a buffer, or still on disk, is really irrelevant.

    请注意,输入的缓冲有些复杂,因为它会在几个不同的级别上出现
    ,而在操作系统级别上,根据设备的不同,它会采用不同的
    形式。通常,操作系统会按
    个扇区缓冲文件,通常会提前读取几个扇区。除非遇到
    文件结尾,否则操作系统将始终按要求返回
    个字符。大多数操作系统将按行缓冲键盘:在输入完整行之前,不会从
    读请求中返回,并且在读请求中从不返回当前行末尾的
    个字符。

    Note that the buffering of input is somewhat complex, in that it occurs at several different levels, and at the OS level, it takes different forms depending on the device. Typically, the OS will buffer a file by sectors, often reading several sectors in advance. The OS will always return as many characters as were demanded, unless it encounters end of file. Most OSs will buffer a keyboard by line: not returning from a read request until a complete line has been entered, and never returning characters beyond the end of the current line in a read request.

    std :: ostream 相同,使用 streambuf 对于输出,
    std :: istream 使用一个来获取每个字符。对于
    std :: cin 的情况,通常是 filebuf ;当 istream
    请求一个字符时,如果
    filebuf 将从其缓冲区返回一个它有一个;如果没有,它将尝试重新填充缓冲区,
    请求例如
    操作系统中的512个字符(或其缓冲区大小不限)。如上所述,它将根据其对
    设备的缓冲策略作出响应。

    In the same manner as std::ostream uses a streambuf for output, std::istream uses one to get each individual character. In the case of std::cin, it will normally be a filebuf; when the istream requests a character, the filebuf will return one from its buffer if it has one; if it doesn't, it will attempt to refill the buffer, requesting e.g. 512 (or whatever its buffer size is) characters from the OS. Which will respond according to its buffering policy for the device, as described above.

    无论如何,如果 std :: cin 已连接到键盘,并且您已经
    键入了 hello world ,您键入的所有字符都将是最终在信息流中读取
    。 (但是,如果您使用的是>> ,则会有很多
    的空白,不会看到)

    At any rate, if std::cin is connected to the keyboard, and you've typed "hello world", all of the characters you've typed will be read by the stream eventually. (But if you're using >>, there'll be a lot of whitespace that you won't see.)

    这篇关于缓冲流和非缓冲流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆