在C ++中从文件读取时动态分配内存以构造 [英] dynamically allocating memory to struct when reading from file in C++

查看:142
本文介绍了在C ++中从文件读取时动态分配内存以构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构

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

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

And I'm writing its contents to a binary file.

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

I write at different times and have many data on file that are written from this struct.

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

Now, I want to read ALL the data that is there on the binary file on to the struct. I'm not sure how I can allocate memory(dynamically) to the struct so that the struct can accommodate all the data on to the struct.

可以

代码:

#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()方法。

You should use a vector<student_t> instead of old-style arrays. It will handle dynamic allocation (use push_back() to add items) and you can get its size with the size() method.

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

For the file reading you can do something like this:

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
    }
}

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

Don't forget to add the binary options too.

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

For the name inside your student_t struct, it would be much easier to declare it as a string. That way, you wouldn't have to use strcpy and the like and you could just type mystudent.name = "jimmy"

这篇关于在C ++中从文件读取时动态分配内存以构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆