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

          <bdo id='G4iMA'></bdo><ul id='G4iMA'></ul>

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

      1. <legend id='G4iMA'><style id='G4iMA'><dir id='G4iMA'><q id='G4iMA'></q></dir></style></legend>

        正确读取和写入 std::vector 到文件中

        Reading and writing a std::vector into a file correctly(正确读取和写入 std::vector 到文件中)
        • <legend id='QmPfy'><style id='QmPfy'><dir id='QmPfy'><q id='QmPfy'></q></dir></style></legend>
          <i id='QmPfy'><tr id='QmPfy'><dt id='QmPfy'><q id='QmPfy'><span id='QmPfy'><b id='QmPfy'><form id='QmPfy'><ins id='QmPfy'></ins><ul id='QmPfy'></ul><sub id='QmPfy'></sub></form><legend id='QmPfy'></legend><bdo id='QmPfy'><pre id='QmPfy'><center id='QmPfy'></center></pre></bdo></b><th id='QmPfy'></th></span></q></dt></tr></i><div id='QmPfy'><tfoot id='QmPfy'></tfoot><dl id='QmPfy'><fieldset id='QmPfy'></fieldset></dl></div>

          <tfoot id='QmPfy'></tfoot>

                <tbody id='QmPfy'></tbody>
                <bdo id='QmPfy'></bdo><ul id='QmPfy'></ul>

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

                • 本文介绍了正确读取和写入 std::vector 到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这就是重点.如何写入和读取其中包含 std::vector 的二进制文件?

                  That is the point. How to write and read binary files with std::vector inside them?

                  我在想:

                  //============ WRITING A VECTOR INTO A FILE ================
                  const int DIM = 6;
                  int array[DIM] = {1,2,3,4,5,6};
                  std::vector<int> myVector(array, array + DIM);
                  ofstream FILE(Path, ios::out | ofstream::binary);
                  FILE.write(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6);
                  //===========================================================
                  

                  但我不知道如何阅读这个向量.因为我认为以下是正确的,但事实并非如此:

                  But I don't know how to read this vector. Because I thought that the following was correctly but it isn't:

                  ifstream FILE(Path, ios::in | ifstream::binary);
                  FILE.read(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6);
                  

                  那么,如何执行操作?

                  推荐答案

                  尝试使用 ostream_iterator/ostreambuf_iterator, istream_iterator/istreambuf_iterator 和 STL copy 方法:

                  Try using an ostream_iterator/ostreambuf_iterator, istream_iterator/istreambuf_iterator, and the STL copy methods:

                  #include <algorithm>
                  #include <iostream>
                  #include <iterator>
                  #include <vector>
                  
                  #include <fstream> // looks like we need this too (edit by π)
                  
                  std::string path("/some/path/here");
                  
                  const int DIM = 6;
                  int array[DIM] = {1,2,3,4,5,6};
                  std::vector<int> myVector(array, array + DIM);
                  std::vector<int> newVector;
                  
                  std::ofstream FILE(path, std::ios::out | std::ofstream::binary);
                  std::copy(myVector.begin(), myVector.end(), std::ostreambuf_iterator<char>(FILE));
                  
                  std::ifstream INFILE(path, std::ios::in | std::ifstream::binary);
                  std::istreambuf_iterator iter(INFILE);
                  std::copy(iter.begin(), iter.end(), std::back_inserter(newVector));
                  

                  这篇关于正确读取和写入 std::vector 到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

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

                    <tfoot id='t7wx9'></tfoot>

                      <tbody id='t7wx9'></tbody>

                      <bdo id='t7wx9'></bdo><ul id='t7wx9'></ul>
                        <legend id='t7wx9'><style id='t7wx9'><dir id='t7wx9'><q id='t7wx9'></q></dir></style></legend>

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