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

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

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

        • <bdo id='yUw7R'></bdo><ul id='yUw7R'></ul>

        如何从内存中读取,就像使用 iostream 从文件中读

        时间:2023-09-15

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

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

                1. 本文介绍了如何从内存中读取,就像使用 iostream 从文件中读取一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我将简单的文本文件加载到内存中.我想从内存中读取,就像从这里读取光盘一样:

                  ifstream 文件;字符串线;file.open("C:\file.txt");如果(文件.is_open()){而(文件.好()){获取行(文件,行);}}file.close();

                  但我在内存中有文件.我在内存中有一个地址和这个文件的大小.

                  我必须做什么才能像上面代码中处理文件一样流畅?

                  解决方案

                  我找到了一个适用于 VC++ 的解决方案,因为 Nim 解决方案仅适用于 GCC 编译器(不过非常感谢.感谢您的回答,我找到了其他答案帮助了我!).

                  其他人似乎也有类似的问题.我完全按照这里和此处.>

                  所以要像形成 istream 一样从一块内存中读取,你必须这样做:

                  class membuf : 公共流缓冲{上市:membuf(char* p, size_t n) {setg(p, p, p + n);}};int main(){char buffer[] = "Hello World!
                  这是下一行
                  最后一行";membuf mb(缓冲区,大小(缓冲区));istream istr(&mb);字符串线;while(getline(istr, line)){cout<<行:["<<线<<"]" <<结束;}}

                  如果你有 ' ' 新行,就像 Nim 写的那样:

                  if (*line.rbegin() == '
                  ') line.erase(line.end() - 1);

                  <小时>

                  我试图将此内存视为 wistream.有人知道怎么做这个吗?我问了单独的问题 为此.

                  I have simple text file loaded into memory. I want to read from memory just like I would read from a disc like here:

                  ifstream file;
                  string line;
                  
                  file.open("C:\file.txt");
                  if(file.is_open())
                  {
                      while(file.good())
                      {
                          getline(file,line);         
                      }
                  }   
                  file.close();
                  

                  But I have file in memory. I have an address in memory and a size of this file.

                  What I must do to have the same fluency as with dealing with file in the code above?

                  解决方案

                  I found a solution that works on VC++ since Nim solution works only on GCC compiler (big thanks, though. Thanks to your answer I found other answers which helped me!).

                  It seems that other people have similar problem too. I did exactly as here and here.

                  So to read from a piece of memory just like form a istream you have to do this:

                  class membuf : public streambuf
                  {
                      public:
                          membuf(char* p, size_t n) {
                          setg(p, p, p + n);
                      }
                  };
                  
                  int main()
                  {
                      char buffer[] = "Hello World!
                  This is next line
                  The last line";  
                      membuf mb(buffer, sizeof(buffer));
                  
                      istream istr(&mb);
                      string line;
                      while(getline(istr, line))
                      {
                          cout << "line:[" << line << "]" << endl;
                      }
                  }
                  

                  EDIT: And if you have ' ' new lines do as Nim wrote:

                  if (*line.rbegin() == '
                  ') line.erase(line.end() - 1);
                  


                  I'm trying to treat this memory as as wistream. Does anybody know how to do this? I asked separate question for this.

                  这篇关于如何从内存中读取,就像使用 iostream 从文件中读取一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在内存中的特定位置存储值? 下一篇:未记录的 GCC 扩展:结构中的 VLA

                  相关文章

                  最新文章

                  <small id='35LN9'></small><noframes id='35LN9'>

                  <legend id='35LN9'><style id='35LN9'><dir id='35LN9'><q id='35LN9'></q></dir></style></legend>

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

                        <bdo id='35LN9'></bdo><ul id='35LN9'></ul>
                      <tfoot id='35LN9'></tfoot>