c++ - 如何读/写二进制文件中的结构?

标签 c++ vector struct binaryfiles

我面临一个小问题。我有一个结构,它有一个 vector 。请注意, vector 在每次迭代时都是动态的。现在,在特定的迭代中,如何将包含大小为 n 的 vector 的结构存储到二进制文件中?

另外,在检索时,假设我知道 vector 的大小,如何从二进制文件中检索包含所有存储元素的 vector 的结构变量?

我能够将某些内容存储到二进制文件中(因为我可以看到写入时大小增加),但是当我尝试取回元素时, vector 的大小为零。

很遗憾,我必须使用标准 STL 来实现这一点,而不是使用任何第三方库。

最佳答案

你应该看看 Boost Serialization .

如果你不能使用 3rd 方库,你必须知道 C++ 不直接支持序列化。这意味着你必须自己做。

This article展示了一种将自定义对象序列化到磁盘并将其取回的好方法。和this tutorial向您展示如何立即开始使用 fstream

这是我的尝试:

编辑:由于 OP 询问如何存储/检索比记录更多的信息,我决定更新原始代码。

那么,发生了什么变化?现在有一个 array student_t apprentice[3]; 来存储 3 个学生的信息。整个数组被序列化到磁盘,然后全部加载回 RAM,在那里可以读取/搜索特定记录。请注意,这是一个非常非常小的文件(84 字节)。在大文件中搜索记录时,我不建议使用这种方法。

#include <fstream>
#include <iostream>
#include <vector>
#include <string.h>

using namespace std;


typedef struct student
{
    char name[10];
    int age;
    vector<int> grades;
}student_t;

int main()
{
    student_t apprentice[3];  
    strcpy(apprentice[0].name, "john");
    apprentice[0].age = 21;
    apprentice[0].grades.push_back(1);
    apprentice[0].grades.push_back(3);
    apprentice[0].grades.push_back(5);    

    strcpy(apprentice[1].name, "jerry");
    apprentice[1].age = 22;
    apprentice[1].grades.push_back(2);
    apprentice[1].grades.push_back(4);
    apprentice[1].grades.push_back(6);

    strcpy(apprentice[2].name, "jimmy");
    apprentice[2].age = 23;
    apprentice[2].grades.push_back(8);
    apprentice[2].grades.push_back(9);
    apprentice[2].grades.push_back(10);

    // Serializing struct to student.data
    ofstream output_file("students.data", ios::binary);
    output_file.write((char*)&apprentice, sizeof(apprentice));
    output_file.close();

    // Reading from it
    ifstream input_file("students.data", ios::binary);
    student_t master[3];
    input_file.read((char*)&master, sizeof(master));         

    for (size_t idx = 0; idx < 3; idx++)
    {
        // If you wanted to search for specific records, 
        // you should do it here! if (idx == 2) ...

        cout << "Record #" << idx << endl;
        cout << "Name: " << master[idx].name << endl;
        cout << "Age: " << master[idx].age << endl;
        cout << "Grades: " << endl;
        for (size_t i = 0; i < master[idx].grades.size(); i++)
           cout << master[idx].grades[i] << " ";
        cout << endl << endl;
    }

    return 0;
}

输出:

Record #0
Name: john
Age: 21
Grades: 
1 3 5 

Record #1
Name: jerry
Age: 22
Grades: 
2 4 6 

Record #2
Name: jimmy
Age: 23
Grades: 
8 9 10

二进制文件的转储:

$ hexdump -c students.data 
0000000   j   o   h   n  \0 237   {  \0   �   �   {   � 025  \0  \0  \0
0000010   (   �   �  \b   4   �   �  \b   8   �   �  \b   j   e   r   r
0000020   y  \0   �  \0   �   �   |  \0 026  \0  \0  \0   @   �   �  \b
0000030   L   �   �  \b   P   �   �  \b   j   i   m   m   y  \0  \0  \0
0000040   �   6   �  \0 027  \0  \0  \0   X   �   �  \b   d   �   �  \b
0000050   h   �   �  \b                                                
0000054

关于c++ - 如何读/写二进制文件中的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5506645/

相关文章:

c++ - std::list 接口(interface)内存泄漏

c++ - 标准 vector 性能/替代

c++ - g++ : can't link with a main executable file

c - struct - 使用 sizeof() 的最佳方法

c++ - 在 C++11 中,如何调用 new 并为对象预留足够的内存?

c++ - 转换不同参数类型的二元运算函数

struct - 如何解决 "type interface has no field or method"错误?

c# - Unity - 在检查器中基于结构字段的自定义结构名称

c++ - 有没有一些工具可以删除MFC项目中的冗余资源?

c++ - 从迭代器返回对象的引用