用c语言设计一个小型管理信息程序,要求使用文件作为信息的外部存储形式
时间: 2024-02-14 07:02:54 浏览: 24
好的,我可以为您提供一个简单的C语言代码示例来实现基本的管理信息程序,并使用文件作为信息的外部存储形式。
以下是代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义一个结构体来存储学生信息
struct Student {
char name[20];
int id;
char gender;
int age;
};
// 定义函数来实现添加学生信息
void addStudent(struct Student student) {
// 打开文件,以追加的方式写入
FILE *fp = fopen("students.dat", "ab");
// 将学生信息写入文件
fwrite(&student, sizeof(struct Student), 1, fp);
// 关闭文件
fclose(fp);
}
// 定义函数来实现删除学生信息
void deleteStudent(int id) {
// 打开文件,以读写的方式打开
FILE *fp = fopen("students.dat", "rb+");
// 读取文件中的学生信息,找到要删除的学生信息
struct Student student;
while(fread(&student, sizeof(struct Student), 1, fp)) {
if(student.id == id) {
// 将文件指针移动到要删除的学生信息的位置
fseek(fp, -sizeof(struct Student), SEEK_CUR);
// 将要删除的学生信息的id设置为-1,标识该信息已被删除
student.id = -1;
// 将修改后的学生信息写回文件
fwrite(&student, sizeof(struct Student), 1, fp);
break;
}
}
// 关闭文件
fclose(fp);
}
// 定义函数来实现修改学生信息
void modifyStudent(int id) {
// 打开文件,以读写的方式打开
FILE *fp = fopen("students.dat", "rb+");
// 读取文件中的学生信息,找到要修改的学生信息
struct Student student;
while(fread(&student, sizeof(struct Student), 1, fp)) {
if(student.id == id) {
// 获取用户输入的新的学生信息
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生性别:");
scanf(" %c", &student.gender);
printf("请输入学生年龄:");
scanf("%d", &student.age);
// 将文件指针移动到要修改的学生信息的位置
fseek(fp, -sizeof(struct Student), SEEK_CUR);
// 将修改后的学生信息写回文件
fwrite(&student, sizeof(struct Student), 1, fp);
break;
}
}
// 关闭文件
fclose(fp);
}
// 定义函数来实现查询学生信息
void queryStudent(int id) {
// 打开文件,以只读的方式打开
FILE *fp = fopen("students.dat", "rb");
// 读取文件中的学生信息,找到要查询的学生信息
struct Student student;
while(fread(&student, sizeof(struct Student), 1, fp)) {
if(student.id == id) {
// 输出要查询的学生信息
printf("学生姓名:%s\n", student.name);
printf("学生学号:%d\n", student.id);
printf("学生性别:%c\n", student.gender);
printf("学生年龄:%d\n", student.age);
break;
}
}
// 关闭文件
fclose(fp);
}
int main() {
// 添加学生信息
struct Student student1 = {"张三", 1001, 'M', 18};
addStudent(student1);
struct Student student2 = {"李四", 1002, 'F', 19};
addStudent(student2);
struct Student student3 = {"王五", 1003, 'M', 20};
addStudent(student3);
// 删除学生信息
deleteStudent(1002);
// 修改学生信息
modifyStudent(1003);
// 查询学生信息
queryStudent(1001);
return 0;
}
```
在这个示例中,我们首先定义了一个结构体`Student`来存储学生信息。然后,我们实现了四个函数来实现添加、删除、修改和查询学生信息的操作。在每个操作中,我们都打开了一个名为“students.dat”的文件,然后根据不同的操作实现数据的读写。最后,我们在`main()`函数中调用了这四个函数来演示程序的基本功能。
希望这个示例可以帮助到您,如果您有任何问题,请随时问我。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)