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

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

      <tfoot id='GKByB'></tfoot>

      1. 设置标准流使用的内部缓冲区 (pubsetbuf)

        Setting the internal buffer used by a standard stream (pubsetbuf)(设置标准流使用的内部缓冲区 (pubsetbuf))

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

          <legend id='A5n96'><style id='A5n96'><dir id='A5n96'><q id='A5n96'></q></dir></style></legend>

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

                <tbody id='A5n96'></tbody>

            • <tfoot id='A5n96'></tfoot>

                <bdo id='A5n96'></bdo><ul id='A5n96'></ul>
                • 本文介绍了设置标准流使用的内部缓冲区 (pubsetbuf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个需要将数据写入现有缓冲区的子例程,我想使用 stringstream 类,方便数据的格式化.

                  I'm writing a subroutine that needs to write data to an existing buffer, and I would like to use the stringstream class to facilitate the formatting of the data.

                  最初,我使用以下代码将流的内容复制到缓冲区中,但希望避免这种解决方案,因为它复制了太多数据.

                  Initially, I used the following code to copy the contents of the stream into the buffer, but would like to avoid this solution as it copies too much data.

                  #include <sstream>
                  #include <algorithm>
                  
                  void FillBuffer(char* buffer, unsigned int size)
                  {
                      std::stringstream message;
                      message << "Hello" << std::endl;
                      message << "World!" << std::endl;
                  
                      std::string messageText(message.str());
                      std::copy(messageText.begin(), messageText.end(), buffer);
                  }
                  

                  这是我发现 streambuf::pubsetbuf() 方法,简单地将上面的代码改写如下.

                  This is when I discovered the streambuf::pubsetbuf() method and simply rewrote the above code as follows.

                  #include <sstream>
                  
                  void FillBuffer(char* buffer, unsigned int size)
                  {
                      std::stringstream message;
                      message.rdbuf()->pubsetbuf(buffer, size);
                  
                      message << "Hello" << std::endl;
                      message << "World!" << std::endl;
                  }
                  

                  不幸的是,这在 Visual Studio 2008 附带的 C++ 标准库实现下不起作用;buffer 保持不变.

                  Unfortunately, this does not work under the C++ standard library implementation that ships with Visual Studio 2008; buffer remains unchanged.

                  我查看了 pubsetbuf 的实现,结果发现它实际上什么都不做".

                  I looked at the implementation of pubsetbuf and it turns out that it literally "does nothing".

                  virtual _Myt *__CLR_OR_THIS_CALL setbuf(_Elem *, streamsize)
                  {   // offer buffer to external agent (do nothing)
                      return (this);
                  }
                  

                  这似乎是给定 C++ 标准库实现的限制.配置流以将其内容写入给定缓冲区的推荐方法是什么?

                  This appears to be a limitation of the given C++ standard library implementation. What is the recommended way to configure a stream to write its contents to a given buffer?

                  推荐答案

                  在对这个问题进行更多研究并仔细检查我的代码后,我遇到了 一篇帖子 建议使用手动编码std::streambuf 类.这段代码背后的想法是创建一个 streambuf 来初始化它的内部以引用给定的缓冲区.代码如下.

                  After some more research on this problem, and scrutiny of my code, I came across a post suggesting the use of a hand-coded std::streambuf class. The idea behind this code is to create a streambuf that initializes its internals to refer to the given buffer. The code is as follows.

                  #include <streambuf>
                  
                  template <typename char_type>
                  struct ostreambuf : public std::basic_streambuf<char_type, std::char_traits<char_type> >
                  {
                      ostreambuf(char_type* buffer, std::streamsize bufferLength)
                      {
                          // set the "put" pointer the start of the buffer and record it's length.
                          setp(buffer, buffer + bufferLength);
                      }
                  };
                  

                  现在,如果您查看我的原始代码,你会注意到我并不是真的需要一个 stringstream 开始.我真正需要的是一种使用 IOStream 库和std::ostream 是一种更好的寻址类型这个问题.顺便说一句,我怀疑这就是 array_sink 类型来自 Boost.IOStreams 已实施.

                  Now if you look at my original code, you will notice that I didn't really need a stringstream to begin with. All I really needed was a way to write to an external buffer using the IOStream library and std::ostream is a much better type to address this problem. Incidentally, I suspect this is how the array_sink type from Boost.IOStreams is implemented.

                  这是使用我的 ostreambuf 类型的修改后的代码.

                  Here is the modified code that uses my ostreambuf type.

                  #include <ostream>
                  #include "ostreambuf.h"  // file including ostreambuf struct from above.
                  
                  void FillBuffer(char* buffer, unsigned int size)
                  {
                      ostreambuf<char> ostreamBuffer(buffer, size);
                      std::ostream messageStream(&ostreamBuffer);
                  
                      messageStream << "Hello" << std::endl;
                      messageStream << "World!" << std::endl;
                  }
                  

                  这篇关于设置标准流使用的内部缓冲区 (pubsetbuf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                  1. <tfoot id='hINBB'></tfoot>

                    <legend id='hINBB'><style id='hINBB'><dir id='hINBB'><q id='hINBB'></q></dir></style></legend>

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

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

                            <tbody id='hINBB'></tbody>