请给我一个用C语言编写的完程序,要求:1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:序号、姓名、地址、学号,每排每列对齐,没有对齐的地方用空格补齐。2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑,3.这个文件要有纵横结构,每横表示一个记录4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改,其中学生信息存储在students.dat文件中,每个学生信息占据一行,每个字段用空格分隔,每个字段的最大宽度为MAX_NAME_LEN。代码中实现插入、删除和修改学生信息的功能。能在文件中第一行显示姓名、地址、学号,并且修改后能自动保存的需要能自动保存的
时间: 2024-03-11 18:43:52 浏览: 72
这是一个比较复杂的程序,需要分多个步骤来实现。我将分步骤进行介绍,并提供相应的代码示例。
1. 定义结构体
首先,我们需要定义一个结构体来存储学生信息。结构体包括学生的序号、姓名、地址和学号。代码如下:
```
#define MAX_NAME_LEN 20
typedef struct {
int id;
char name[MAX_NAME_LEN];
char address[MAX_NAME_LEN];
char student_id[MAX_NAME_LEN];
} Student;
```
2. 读取文件中的学生信息
接下来,我们需要编写一个函数来读取文件中的学生信息。该函数将打开students.dat文件,逐行读取文件中的数据,并将其存储到Student结构体数组中。代码如下:
```
#define MAX_STUDENT_NUM 100
Student students[MAX_STUDENT_NUM];
int student_num = 0;
void read_students() {
FILE* fp = fopen("students.dat", "r");
if (fp == NULL) {
printf("Failed to open file!");
return;
}
while (fscanf(fp, "%d %s %s %s", &students[student_num].id, students[student_num].name, students[student_num].address, students[student_num].student_id) != EOF) {
student_num++;
}
fclose(fp);
}
```
3. 显示学生信息
接下来,我们需要编写一个函数来显示学生信息。该函数将遍历Student结构体数组,逐个输出每个学生的信息。代码如下:
```
void show_students() {
printf("ID\tName\tAddress\tStudent ID\n");
for (int i = 0; i < student_num; i++) {
printf("%d\t%s\t%s\t%s\n", students[i].id, students[i].name, students[i].address, students[i].student_id);
}
}
```
4. 插入学生信息
现在我们来实现插入学生信息的功能。该函数将在Student结构体数组的末尾添加一个新的学生信息。代码如下:
```
void insert_student() {
printf("Please input student information (id name address student_id): ");
scanf("%d %s %s %s", &students[student_num].id, students[student_num].name, students[student_num].address, students[student_num].student_id);
student_num++;
}
```
5. 删除学生信息
接下来,我们来实现删除学生信息的功能。该函数将要求用户输入要删除的学生的ID,然后从Student结构体数组中删除该学生信息。代码如下:
```
void delete_student() {
int id;
printf("Please input the ID of the student you want to delete: ");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
break;
}
}
}
```
6. 修改学生信息
最后,我们来实现修改学生信息的功能。该函数将要求用户输入要修改的学生的ID,然后允许用户修改该学生的姓名、地址和学号。代码如下:
```
void update_student() {
int id;
printf("Please input the ID of the student you want to update: ");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("Please input the new information (name address student_id): ");
scanf("%s %s %s", students[i].name, students[i].address, students[i].student_id);
break;
}
}
}
```
7. 保存学生信息
最后,我们需要编写一个函数来保存学生信息。该函数将把Student结构体数组中的数据写入students.dat文件中。代码如下:
```
void save_students() {
FILE* fp = fopen("students.dat", "w");
if (fp == NULL) {
printf("Failed to open file!");
return;
}
for (int i = 0; i < student_num; i++) {
fprintf(fp, "%d %s %s %s\n", students[i].id, students[i].name, students[i].address, students[i].student_id);
}
fclose(fp);
}
```
完整代码如下:
```
#include <stdio.h>
#define MAX_NAME_LEN 20
#define MAX_STUDENT_NUM 100
typedef struct {
int id;
char name[MAX_NAME_LEN];
char address[MAX_NAME_LEN];
char student_id[MAX_NAME_LEN];
} Student;
Student students[MAX_STUDENT_NUM];
int student_num = 0;
void read_students() {
FILE* fp = fopen("students.dat", "r");
if (fp == NULL) {
printf("Failed to open file!");
return;
}
while (fscanf(fp, "%d %s %s %s", &students[student_num].id, students[student_num].name, students[student_num].address, students[student_num].student_id) != EOF) {
student_num++;
}
fclose(fp);
}
void show_students() {
printf("ID\tName\tAddress\tStudent ID\n");
for (int i = 0; i < student_num; i++) {
printf("%d\t%s\t%s\t%s\n", students[i].id, students[i].name, students[i].address, students[i].student_id);
}
}
void insert_student() {
printf("Please input student information (id name address student_id): ");
scanf("%d %s %s %s", &students[student_num].id, students[student_num].name, students[student_num].address, students[student_num].student_id);
student_num++;
}
void delete_student() {
int id;
printf("Please input the ID of the student you want to delete: ");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
break;
}
}
}
void update_student() {
int id;
printf("Please input the ID of the student you want to update: ");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("Please input the new information (name address student_id): ");
scanf("%s %s %s", students[i].name, students[i].address, students[i].student_id);
break;
}
}
}
void save_students() {
FILE* fp = fopen("students.dat", "w");
if (fp == NULL) {
printf("Failed to open file!");
return;
}
for (int i = 0; i < student_num; i++) {
fprintf(fp, "%d %s %s %s\n", students[i].id, students[i].name, students[i].address, students[i].student_id);
}
fclose(fp);
}
int main() {
read_students();
show_students();
insert_student();
show_students();
delete_student();
show_students();
update_student();
show_students();
save_students();
return 0;
}
```
阅读全文