1. <small id='PFxKd'></small><noframes id='PFxKd'>

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

      如何将文件内容读入 istringstream?

      时间:2023-09-16
        <tbody id='eumde'></tbody>
          <legend id='eumde'><style id='eumde'><dir id='eumde'><q id='eumde'></q></dir></style></legend>

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

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

                本文介绍了如何将文件内容读入 istringstream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                为了提高读取文件的性能,我尝试将一个大(几 MB)文件的全部内容读入内存,然后使用 istringstream 访问信息.

                In order to improve performance reading from a file, I'm trying to read the entire content of a big (several MB) file into memory and then use a istringstream to access the information.

                我的问题是,读取这些信息并将其导入"到字符串流中的最佳方式是什么?这种方法的一个问题(见下文)是,在创建字符串流时,缓冲区被复制,内存使用量加倍.

                My question is, which is the best way to read this information and "import it" into the string stream? A problem with this approach (see bellow) is that when creating the string stream the buffers gets copied, and memory usage doubles.

                #include <fstream>
                #include <sstream>
                
                using namespace std;
                
                int main() {
                  ifstream is;
                  is.open (sFilename.c_str(), ios::binary );
                
                  // get length of file:
                  is.seekg (0, std::ios::end);
                  long length = is.tellg();
                  is.seekg (0, std::ios::beg);
                
                  // allocate memory:
                  char *buffer = new char [length];
                
                  // read data as a block:
                  is.read (buffer,length);
                
                  // create string stream of memory contents
                  // NOTE: this ends up copying the buffer!!!
                  istringstream iss( string( buffer ) );
                
                  // delete temporary buffer
                  delete [] buffer;
                
                  // close filestream
                  is.close();
                
                  /* ==================================
                   * Use iss to access data
                   */
                
                }
                

                推荐答案

                std::ifstream 有一个方法 rdbuf(),它返回一个指向 filebuf.然后你可以把这个 filebuf push"到你的 stringstream 中:

                std::ifstream has a method rdbuf(), that returns a pointer to a filebuf. You can then "push" this filebuf into your stringstream:

                #include <fstream>
                #include <sstream>
                
                int main()
                {
                    std::ifstream file( "myFile" );
                
                    if ( file )
                    {
                        std::stringstream buffer;
                
                        buffer << file.rdbuf();
                
                        file.close();
                
                        // operations on the buffer...
                    }
                }
                

                正如 Martin York 在评论中所说,这可能不是最快的解决方案,因为 stringstreamoperator<< 将逐个字符读取 filebuf.您可能想检查他的答案,他在那里使用 ifstreamread 方法,然后设置 stringstream 缓冲区指向之前分配的内存.

                As Martin York remarks in the comments, this might not be the fastest solution since the stringstream's operator<< will read the filebuf character by character. You might want to check his answer, where he uses the ifstream's read method as you used to do, and then set the stringstream buffer to point to the previously allocated memory.

                这篇关于如何将文件内容读入 istringstream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 g++ 中使用配置文件引导的优化? 下一篇:g++ 中的优化级别 -O3 是否危险?

                相关文章

                最新文章

                    <tfoot id='zalDM'></tfoot>
                  1. <small id='zalDM'></small><noframes id='zalDM'>

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