• <i id='0rXO7'><tr id='0rXO7'><dt id='0rXO7'><q id='0rXO7'><span id='0rXO7'><b id='0rXO7'><form id='0rXO7'><ins id='0rXO7'></ins><ul id='0rXO7'></ul><sub id='0rXO7'></sub></form><legend id='0rXO7'></legend><bdo id='0rXO7'><pre id='0rXO7'><center id='0rXO7'></center></pre></bdo></b><th id='0rXO7'></th></span></q></dt></tr></i><div id='0rXO7'><tfoot id='0rXO7'></tfoot><dl id='0rXO7'><fieldset id='0rXO7'></fieldset></dl></div>

    <small id='0rXO7'></small><noframes id='0rXO7'>

      <bdo id='0rXO7'></bdo><ul id='0rXO7'></ul>
    <tfoot id='0rXO7'></tfoot>

      <legend id='0rXO7'><style id='0rXO7'><dir id='0rXO7'><q id='0rXO7'></q></dir></style></legend>
      1. 缓冲和非缓冲流

        Buffered and unbuffered stream(缓冲和非缓冲流)
        <legend id='bTh3v'><style id='bTh3v'><dir id='bTh3v'><q id='bTh3v'></q></dir></style></legend>
      2. <small id='bTh3v'></small><noframes id='bTh3v'>

          <bdo id='bTh3v'></bdo><ul id='bTh3v'></ul>
            <tfoot id='bTh3v'></tfoot>

            1. <i id='bTh3v'><tr id='bTh3v'><dt id='bTh3v'><q id='bTh3v'><span id='bTh3v'><b id='bTh3v'><form id='bTh3v'><ins id='bTh3v'></ins><ul id='bTh3v'></ul><sub id='bTh3v'></sub></form><legend id='bTh3v'></legend><bdo id='bTh3v'><pre id='bTh3v'><center id='bTh3v'></center></pre></bdo></b><th id='bTh3v'></th></span></q></dt></tr></i><div id='bTh3v'><tfoot id='bTh3v'></tfoot><dl id='bTh3v'><fieldset id='bTh3v'></fieldset></dl></div>
                <tbody id='bTh3v'></tbody>

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

                  问题描述

                  在缓冲流的情况下,它在书中说它等到缓冲区已满才写回监视器.例如:

                  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";
                  

                1. 我的书上说发送到cerr的所有东西都会立即写入标准错误设备,这是什么意思?

                2. 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";
                  

                3. 在这个例子中,ch 将被分配给hello"而world"将被忽略,这是否意味着它仍在缓冲区中并且会影响未来语句的结果?

                4. 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::filebuf(派生自 streambuf)并且 std::ofstream 通常会被缓冲.这意味着当你输出一个字符,不一定立即输出;它会写入缓冲区,仅当缓冲区被写入时才输出到操作系统full,或者您以某种方式明确请求它,通常是通过调用flush()(直接或间接,通过使用 std::endl).然而,这可能会有所不同;std::cout 的输出与stdout,并且大多数实现或多或少会遵循以下规则stdout for 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_basestd::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";
                  

                  流将在字符串中的所有字符之后被刷新是输出,提供 unitbuf 设置.启动时,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.

                  请注意,这里的所有问题都是 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.)

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
                5. 相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                        <bdo id='VAI7x'></bdo><ul id='VAI7x'></ul>
                        <tfoot id='VAI7x'></tfoot>

                        • <legend id='VAI7x'><style id='VAI7x'><dir id='VAI7x'><q id='VAI7x'></q></dir></style></legend>

                            <tbody id='VAI7x'></tbody>

                          <small id='VAI7x'></small><noframes id='VAI7x'>

                        • <i id='VAI7x'><tr id='VAI7x'><dt id='VAI7x'><q id='VAI7x'><span id='VAI7x'><b id='VAI7x'><form id='VAI7x'><ins id='VAI7x'></ins><ul id='VAI7x'></ul><sub id='VAI7x'></sub></form><legend id='VAI7x'></legend><bdo id='VAI7x'><pre id='VAI7x'><center id='VAI7x'></center></pre></bdo></b><th id='VAI7x'></th></span></q></dt></tr></i><div id='VAI7x'><tfoot id='VAI7x'></tfoot><dl id='VAI7x'><fieldset id='VAI7x'></fieldset></dl></div>