<legend id='sFL6Q'><style id='sFL6Q'><dir id='sFL6Q'><q id='sFL6Q'></q></dir></style></legend>

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

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

      <tfoot id='sFL6Q'></tfoot>

      如何在二进制文件中读取/写入结构?

      时间:2023-09-16
          <tbody id='72QM3'></tbody>
        <tfoot id='72QM3'></tfoot>

        <small id='72QM3'></small><noframes id='72QM3'>

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

          • <bdo id='72QM3'></bdo><ul id='72QM3'></ul>

            <legend id='72QM3'><style id='72QM3'><dir id='72QM3'><q id='72QM3'></q></dir></style></legend>

                本文介绍了如何在二进制文件中读取/写入结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                I am facing a small problem. I have a struct, which has a vector. Note that the vector is dynamic per every iteration. Now, in a particular iteration, how do I store the struct which contains a vector of size n to a binary file?

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

                Also, when retrieving, assume that I know how the size of the vector, how to I retrieve from the binary file, the struct variable containing the vector of all the stored elements?

                我能够将一些内容存储到二进制文件中(因为我可以看到写入时大小增加),但是当我尝试检索元素时,我将向量的大小设置为零.

                I am able to store something to the binary file (as I can see the size increasing when writing), but when I am trying to retrieve back the elements, I am getting size of vector to be zero.

                不幸的是,我必须使用标准 STL 来实现这一点,而不是使用任何第三方库.

                推荐答案

                你应该看看 Boost 序列化.

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

                If you can't use 3rd party libraries, you must know that C++ doesn't support serialization directly. This means you'll have to do it yourself.

                这篇文章 展示了一种将自定义对象序列化到磁盘并取回它的好方法.而本教程向您展示了如何立即开始使用fstream.

                This article shows a nice way of serializing a custom object to the disk and retrieving it back. And this tutorial shows you how to get started right now with fstream.

                这是我的尝试:

                EDIT:由于 OP 询问如何存储/检索比记录更多的内容,因此我决定更新原始代码.

                EDIT: since the OP asked how to store/retrieve more than record I decided to updated the original code.

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

                So, what changed? Now there's an array student_t apprentice[3]; to store information of 3 students. The entire array is serialized to the disk and then it's all loaded back to the RAM where reading/searching for specific records is possible. Note that this is a very very small file (84 bytes). I do not suggest this approach when searching records on huge files.

                #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   237   {     �   �   {   � 025      
                0000010   (   �   �     4   �   �     8   �   �     j   e   r   r
                0000020   y     �     �   �   |   026         @   �   �  
                0000030   L   �   �     P   �   �     j   i   m   m   y      
                0000040   �   6   �   027         X   �   �     d   �   �  
                0000050   h   �   �                                                  
                0000054
                

                这篇关于如何在二进制文件中读取/写入结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:pop_back() 返回值? 下一篇:如何使向量的元素唯一?(删除不相邻的重复项)

                相关文章

                最新文章

              • <tfoot id='98OMm'></tfoot>

                <legend id='98OMm'><style id='98OMm'><dir id='98OMm'><q id='98OMm'></q></dir></style></legend>

                  <bdo id='98OMm'></bdo><ul id='98OMm'></ul>

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

                    <small id='98OMm'></small><noframes id='98OMm'>