将结构体数组存入文件
时间: 2024-06-14 11:04:08 浏览: 19
以下是将结构体数组存入文件的示例代码:
```cpp
#include <iostream>
#include <fstream>
struct Person {
std::string name;
int age;
};
int main() {
// 创建结构体数组
Person people[3];
people[0] = {"Alice", 25};
people[1] = {"Bob", 30};
people[2] = {"Charlie", 35};
// 打开文件
std::ofstream file("people.txt");
// 将结构体数组写入文件
for (int i = 0; i < 3; i++) {
file << people[i].name << " " << people[i].age << std::endl;
}
// 关闭文件
file.close();
return 0;
}
```
这段代码创建了一个名为`Person`的结构体,包含了姓名和年龄两个成员变量。然后创建了一个包含3个`Person`结构体的数组`people`,并给数组中的每个元素赋值。接下来,打开名为`people.txt`的文件,并将结构体数组的内容逐行写入文件中。最后,关闭文件。
相关问题
创建结构体数组存入文件中
好的,我可以为您提供一个简单的示例程序来演示如何创建结构体数组并将其存入文件中。以下是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义结构体
typedef struct {
int id;
char name[20];
float score;
} Student;
int main() {
// 创建结构体数组
Student students[3];
students[0].id = 1;
strcpy(students[0].name, "Tom");
students[0].score = 90.5;
students[1].id = 2;
strcpy(students[1].name, "Jane");
students[1].score = 88.0;
students[2].id = 3;
strcpy(students[2].name, "Mike");
students[2].score = 92.0;
// 打开文件
FILE *fp;
fp = fopen("students.dat", "wb");
if (fp == NULL) {
printf("Error opening file!\n");
exit(1);
}
// 将结构体数组写入文件中
fwrite(students, sizeof(Student), 3, fp);
// 关闭文件
fclose(fp);
return 0;
}
```
在上面的示例代码中,我们首先定义了一个名为 Student 的结构体,其中包含了学生的编号、姓名和成绩三个属性。然后,在主函数中,我们创建了一个包含三个元素的结构体数组,并且为每个元素分别赋值。接着,我们打开了一个名为 students.dat 的文件,并将其以二进制写入模式打开。如果文件打开失败,则程序会输出错误信息并退出。接下来,我们使用 fwrite 函数将结构体数组写入文件中。最后,我们关闭了文件并结束了程序的执行。
注意,写入文件时,我们使用了 fwrite 函数,该函数的第一个参数是指向要写入的数据的指针,第二个参数是每个元素的大小,第三个参数是要写入的元素数量,第四个参数是文件指针。在我们的示例中,我们将结构体数组 students 写入文件中,每个元素的大小为 sizeof(Student) ,元素数量为 3。
希望这个示例程序能够帮助您了解如何创建结构体数组并将其存入文件中。
c语言读取txt文件并存入结构体数组
C语言可以通过以下步骤来读取txt文件并存入结构体数组。
首先,我们需要定义一个结构体类型,用来表示txt文件中的数据。假设我们要读取的txt文件中的每一行包含学生的姓名和年龄,我们可以定义一个名为Student的结构体类型,包含name和age两个成员变量。
```c
typedef struct {
char name[50];
int age;
} Student;
```
然后,我们需要打开文件并读取其中的数据。可以使用标准库函数`fopen`来打开txt文件,使用`fgets`函数逐行读取文件内容,并使用`sscanf`函数将读取到的数据存入结构体数组中。
```c
FILE *file = fopen("students.txt", "r");
if (file == NULL) {
printf("无法打开文件\n");
return;
}
Student students[100]; // 假设最多有100个学生
int count = 0; // 记录读取到的学生个数
char line[100];
while (fgets(line, sizeof(line), file) != NULL) {
sscanf(line, "%s%d", students[count].name, &students[count].age);
count++;
}
fclose(file);
```
最后,我们需要关闭文件,并可以通过遍历数组输出读取到的数据,以验证读取是否成功。
```c
for (int i = 0; i < count; i++) {
printf("姓名:%s,年龄:%d\n", students[i].name, students[i].age);
}
```
以上就是用C语言读取txt文件并存入结构体数组的基本步骤。读者可以根据实际需求对代码进行修改和优化。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)