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

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

        c ++从/到二进制文件读/写类

        c++ Read/Write class from/to binary file(c ++从/到二进制文件读/写类)
        <i id='Bg2xM'><tr id='Bg2xM'><dt id='Bg2xM'><q id='Bg2xM'><span id='Bg2xM'><b id='Bg2xM'><form id='Bg2xM'><ins id='Bg2xM'></ins><ul id='Bg2xM'></ul><sub id='Bg2xM'></sub></form><legend id='Bg2xM'></legend><bdo id='Bg2xM'><pre id='Bg2xM'><center id='Bg2xM'></center></pre></bdo></b><th id='Bg2xM'></th></span></q></dt></tr></i><div id='Bg2xM'><tfoot id='Bg2xM'></tfoot><dl id='Bg2xM'><fieldset id='Bg2xM'></fieldset></dl></div>
          <tbody id='Bg2xM'></tbody>

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

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

              • <legend id='Bg2xM'><style id='Bg2xM'><dir id='Bg2xM'><q id='Bg2xM'></q></dir></style></legend>
                • 本文介绍了c ++从/到二进制文件读/写类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要将一个类写入一个二进制文件,然后我需要读回它.

                  I need to write a class to a binary file, and then I need to read it back.

                  我有 TriangleBinaryFile 类,以及其他一些类.我不确定我是写错了还是读错了.读取时出现错误.调试后,我认为它为我的私有变量获取了不适当的数据.如果有人能给我一些有关如何使其正常工作的建议,我将非常高兴.

                  I have Triangle and BinaryFile classes, and some other classes. I am not sure if I am writing incorrectly or reading incorrectly. An error occurs when reading. After debugging, I think that it gets inappropriate data for my private variables. I will be very glad if someone can give me some advice on how to make it work properly.

                  我不确定是否应该粘贴整个代码,所以我会给你一小段代码.以防万一,这里是我的源代码的下载链接:

                  I wasn't sure if I should paste the whole code or not, so I will give you a short snippet of code. Just in case, here is a download link for my source code:

                  https://my.pcloud.com/publink/show?code=XZJ7CYZbsLWLglqsV5jp83csijcEUTFqqpM3k/a>

                  https://my.pcloud.com/publink/show?code=XZJ7CYZbsLWLglqV5p83csijcEUTFqqpM3k

                  我是编程新手,我的英语不太好,所以我为我的错误提前道歉.

                  I am a newbie in programming and I don't speak English very well, so I apologize in advance for my mistakes.

                  class Point
                  {
                  private:
                      int x;
                      int y;
                  };
                  
                  class Figure
                  {
                  private:
                      string name;
                      string type;
                  };
                  
                  class Triangle: public Figure
                  {
                  private:
                      Point p1, p2, p3;
                  };
                  
                  class BinaryFile
                  {
                  private:
                      string FileName;
                      fstream File;
                  public:
                      //...
                      void AddNewFigure(istream& stream)
                      {       
                          File.open(this->FileName, ios::binary | ios::app);
                          if(!this->File)
                          {
                              cerr<<"File error <"<<this->FileName<<">
                  ";
                              exit(1);
                          }
                          Triangle fig;
                          fig.MakeNewFigure(stream);
                          File.write((char*)&fig, sizeof(Triangle));
                          File.close();
                      }
                  
                      Triangle GetTriangle()
                      {
                          Triangle trig;
                          Point p;
                          string str(""); int x(0);
                          File.open(this->FileName, ios::binary | ios::in);
                          if(!this->File)
                          {
                              cerr<<"File error <"<<this->FileName<<">
                  ";
                              exit(1);
                          }
                          File.read((char*)&trig, sizeof(Triangle));
                          File.close();
                          return trig;
                      }
                  };
                  

                  推荐答案

                  答案取决于您这样做是为了了解文件的工作原理,还是保存到文件只是偶然的,您并不关心它是如何工作的.

                  The answer depends on whether you are just doing this to learn how files work or whether saving to the file is just incidental and you don't care how it works.

                  如果您只想保存和恢复内容,而不关心它是如何工作的,那么请使用第三方库.有很多很多.

                  If you just want to get the stuff to save and restore and you don't care how it works then use a third party library. There are many many of them.

                  如果你想学习如何读写文件,那么你需要自己编写读写函数.我制作了一个示例程序来解释它是如何工作的:

                  If you want to learn how to read and write things to files then you will need to make your own read and write functions. I have made a sample program that will explain how it works:

                  #include <string>
                  #include <fstream>
                  #include <iostream>
                  
                  class Point
                  {
                  private:
                      int x;
                      int y;
                  public:
                      Point():x(0),y(0){}
                      Point(int x,int y):x(x),y(y){}
                  
                      void write(std::ostream& f)
                      {
                          // We can just write out the bytes for x and y because
                          // they are primitive types stored in the class
                          f.write( (char*)&x, sizeof(x) );
                          f.write( (char*)&y, sizeof(y) );
                      }
                      void read(std::istream& f)
                      {
                          // We can just read the bytes directly into x and y because
                          // they are primitive types stored in the class
                          f.read( (char*)&x, sizeof(x) );
                          f.read( (char*)&y, sizeof(y) );
                      }
                  };
                  
                  class Figure
                  {
                  private:
                      std::string name;
                      std::string type;
                  public:
                      Figure(){}
                      Figure(std::string name,std::string type):name(name),type(type){}
                  
                      void write(std::ostream& f)
                      {
                          size_t size;
                  
                          // we need to store the data from the string along with the size
                          // because to restore it we need to temporarily read it somewhere
                          // before storing it in the std::string (istream::read() doesn't
                          // read directly to std::string)
                  
                          size = name.size();
                          f.write( (char*)&size, sizeof(size_t) );
                          f.write( (char*)name.c_str(), size );
                  
                          size = type.size();
                          f.write( (char*)&size, sizeof(size_t) );
                          f.write( (char*)type.c_str(), size );
                      }
                      void read(std::istream& f)
                      {
                          size_t size;
                          char *data;
                  
                          // when we read the string data we need somewhere to store it
                          // because we std::string isn't a primitive type.  So we read
                          // the size, allocate an array, read the data into the array,
                          // load the std::string, and delete the array
                  
                          f.read( (char*)&size, sizeof(size) );
                          data = new char[size+1];
                          f.read( data, size );
                          data[size]='';
                          name = data;
                          delete data;
                  
                          f.read( (char*)&size, sizeof(size) );
                          data = new char[size+1];
                          f.read( data, size );
                          data[size]='';
                          type = data;
                          delete data;
                      }
                  };
                  
                  class Triangle: public Figure
                  {
                  private:
                      Point p1, p2, p3;
                  public:
                      Triangle(){}
                      Triangle(Point x,Point y,Point z,Figure f):p1(x),p2(y),p3(z),Figure(f){}
                  
                  
                      void write(std::ostream& f)
                      {
                          // First write the base class then write the members of this class
                          Figure::write(f);
                          p1.write(f);
                          p2.write(f);
                          p3.write(f);
                      }
                      void read(std::istream& f)
                      {
                          // First read the base class then read the members of this class
                          Figure::read(f);
                          p1.read(f);
                          p2.read(f);
                          p3.read(f);
                      }
                  };
                  
                  class BinaryFile
                  {
                  private:
                      std::string FileName;
                      std::fstream File;
                  public:
                      BinaryFile(std::string FileName) : FileName(FileName) {};
                      void WriteTriangle()
                      {
                          File.open(FileName, std::ios::binary | std::ios::out);
                          if(!File)
                          {
                              std::cerr<<"File error <"<<FileName<<">
                  ";
                              exit(1);
                          }
                          Triangle trig({1,2},{3,4},{5,6},{"name","type"}); // something new
                          trig.write(File);
                          File.close();
                      }
                  
                      Triangle ReadTriangle()
                      {
                          File.open(FileName, std::ios::binary | std::ios::in);
                          if(!File)
                          {
                              std::cerr<<"File error <"<<FileName<<">
                  ";
                              exit(1);
                          }
                          Triangle trig; // default values
                          trig.read(File);
                          File.close();
                          return trig;
                      }
                  };
                  
                  main()
                  {
                      BinaryFile bin("file.bin");
                      bin.WriteTriangle();
                      Triangle trig = bin.ReadTriangle();
                      // at this point trig has the values we stored
                      return 0;
                  }
                  

                  这篇关于c ++从/到二进制文件读/写类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 中生成随机数的最佳方法是什么?)
                    <tbody id='UgRgl'></tbody>
                    <bdo id='UgRgl'></bdo><ul id='UgRgl'></ul>
                    <tfoot id='UgRgl'></tfoot>

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

                        • <small id='UgRgl'></small><noframes id='UgRgl'>

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