从C++中的文件读取时动态分配内存到结构

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

本文关键字:动态分配 内存 结构 读取 C++ 文件      更新时间:2023-10-16

我有一个结构体

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"

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

2 13 <DATA> 8 <DATA>

第一个数字 (2) 表示文件中存储的结构数量。然后数据块随之而来。每个数据块都以一个数字开头,该数字指定grades向量的大小(第一个结构为 13,第二个结构为 8)。

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

等等,等等,直到你读完所有内容。

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

最直接的方法是解包向量,这样写入文件的内容就不是向量,而是整数数组以及数组中的整数。

因此,第一步是编写具有标准、不变结构的第一部分,然后编写一个数字,指示将遵循的整数数量,最后迭代向量将整数写入文件。

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