代码之家  ›  专栏  ›  技术社区  ›  Master-Antonio

在struct中使用struct读/写二进制文件

  •  0
  • Master-Antonio  · 技术社区  · 7 年前

    我需要创建一个二进制文件并写入所创建结构的当前状态。

    此结构具有其他结构。 类似这样:

    struct A {
        bool flag=true;
        int n;
    };
    
    struct B
    {
        int n1;
        A *array;
    };
    

    如何在二进制文件中写入所有内容?

    但特别是如何从二进制文件中读取并加载 struct B 再次,然后全部加载 A *array .

    谢谢。

    我试过这样做,但有时会给我一个例外,并阻止所有 (违反读取权限)。

    如何写入:

    ofstream save("binary.bin", ios::binary);
        if (!save) {
            cerr << "Error creation file ";
        }
        else {
            save.write(reinterpret_cast<char*>(&prova), sizeof(prova));
        }
    
        binary.close();
    

    如何阅读和打印:

    save.read(reinterpret_cast<char*>(&prova), sizeof(prova));
    for (size_t i = 0; i != try.n1; ++i)
       {
       cout << "....." << try.n1 <<endl;
       cout << "..... "<< try.array[i].n <<" ";
       cout << ".....: "<<try.array[i].flag<< endl;
      }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   R Sahu    7 年前

    正在写入的单个实例 A 二进制文件很简单,因为它是POD类型:

    std::ofstream outfile = {...};
    A a = { ... };
    outfile.write(reinterpret_cast<char const*>(&a), sizeof(a));
    

    编写的单个实例 B ,OTOH,不是那么简单。你得写信 n1 首先是数组中的元素,然后是数组中的元素。

    B b = { ... };
    
    // Write the number of elements first.
    outfile.write(reinterpret_cast<char const*>(&b.n1), sizeof(b.n1));
    
    // Now write the elements.
    outfile.write(reinterpret_cast<char const*>(b.array), sizeof(A)*b.n1);
    

    显然,从二进制文件读取是相反的。正在读取的实例 A. 很简单。

    std:ifstream infile = { ... };
    
    A a;
    infile.read(retinterpret_cast<char*>(&a), sizeof(a));
    

    正在读取的实例 B 有点复杂。您必须先读取大小,然后为数组的元素分配内存,然后读取数组的元素。

    B b;
    
    // Read the number of elements
    infile.read(retinterpret_cast<char*>(&b.n1), sizeof(b.n1));
    
    // Allocate memory for the elements of the array.
    b.array = new A[b.n1];
    
    // Now read the elements of the array.
    infile.read(retinterpret_cast<char*>(b.array), sizeof(A)*b.n1);
    

    您可以通过使用助手函数来简化其中一些代码,从而减少 retinterpret_cast 所有代码。

    namespace MyApp
    {
       template <typename T>
       std::ostream& write(std::ostream& out, T const& pod)
       {
          out.write(reinterpret_cast<char const*>(&pod), sizeof(T));
          return out;
       }
    
       template <typename T>
       std::ostream& write(std::ostream& out, T* array, int numElements)
       {
          out.write(reinterpret_cast<char const*>(array), sizeof(T)*numElements);
          return out;
       }
    
       template <typename T>
       std:istream read(std::istream& in, T& pod)
       {
          in.read(reinterpret_cast<char*>(&pod), sizeof(T));
          return in;
       }
    
       template <typename T>
       std:istream& read(std:istream& in, T* array, int numElements)
       {
          in.read(reinterpret_cast<char*>(array), sizeof(T)*numElements);
          return in;
       }
    }
    

    现在,您可以使用:

    // Write a
    MyApp::write(outfile, a);
    
    // Read a
    MyApp::read(infile, a);
    
    // Write b
    MyApp::write(outfile, b.n1);
    MyApp::write(outfile, b.array, b.n1);
    
    // Read b
    MyApp::write(infile, b.n1);
    b.array = new A[b.n1];
    MyApp::write(infile, b.array, b.n1);