繁体   English   中英

在C ++中从文件读取时动态地将内存分配给结构

[英]dynamically allocating memory to struct when reading from file in C++

我有一个结构

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

我正在将其内容写入二进制文件。

我在不同的时间编写文件,并且从该结构写入的文件上有很多数据。

现在,我想读取结构上二进制文件上的所有数据。 我不确定如何将内存动态分配给该结构,以便该结构可以容纳该结构上的所有数据。

你能帮我这个忙吗?

码:

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

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;

    input_file.seekg (0, ios::end);
    cout << input_file.tellg();

    std::vector<student_t> s;

    // input_file.read((char*)s, sizeof(s)); - dint work

    /*input_file >> std::noskipws;
    std::copy(istream_iterator(input_file), istream_iterator(), std::back_inserter(s));*/

    while(input_file >> master) // throws error
    {
        s.push_back(master);
    }
    return 0;
}

您应该使用vector<student_t>而不是旧式数组。 它将处理动态分配(使用push_back()添加项目),您可以使用size()方法获取其大小。

编辑:对于文件读取,您可以执行以下操作:

ifstream myfile;
myfile.open(file_name);
if (myfile.is_open()) {
    while (myfile) {
        string s;
        getline(myfile, s);
        // Do something with the line
        // Push information into students vector
    }
}

不要忘记也添加二进制选项。

对于您的student_t结构中的name ,将其声明为string会容易得多。 这样,您不必使用strcpy之类的东西,您只需键入mystudent.name = "jimmy"

最直接的方法是解包向量,以便您写入文件的内容不是向量,而是一个整数数组以及该数组中整数的数量。

因此,第一步是编写具有标准不变结构的第一部分,然后编写一个数字,该数字表示将要遵循的整数的数量,然后最终遍历将整数写入文件的向量。

然后,当您读取文件时,将使用空向量创建结构,读取结构化且不变的结构的第一部分,然后读取将要放置在向量中的int数,然后读取该int数从文件中。 从文件读取int时,会将它们添加到结构中的向量中。

您需要为此发明一种文件格式。 在文件的开头,您将存储一个所谓的“标题”,其中包含有关其中包含的数据的信息。 例如:

2 13 <DATA> 8 <DATA>

第一个数字(2)给出文件中存储的结构数量。 然后是数据块。 每个数据块均以一个指定grades矢量大小的数字开头(第一个结构为13,第二个结构为8)。

在这种情况下,您将从文件开头读取一个int。 现在您知道该文件中保存了2个结构。 然后,您读取下一个int,在这种情况下为13。 这表明您需要一个容量为13的向量。可以创建一个向量,然后读取所有值。 您将知道何时停止,因为您知道此结构中有多少数据:10个字符(名称),1个整数(年龄),13个整数(等级)。 读完所有内容之后,您便知道下一个int将成为文件中下一个struct的一部分。 它是数字8,它告诉您下一个结构需要一个容量为8的向量。

等等,直到您阅读完所有内容。

请注意,这种二进制文件I / O方法不可移植。 有两个原因。 首先,不同平台之间int的大小可能会有所不同。 其次,以二进制形式存储int(以及其他大于单个字节的数据)的方式也可能有所不同,即使它们的大小相同(有关说明,请参见http://en.wikipedia.org/wiki/Endianness) 。)但是,如果您不打算将程序及其生成的文件移植为可移植的,那么上面介绍的方法就足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM