要求用C语言编写:1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:姓名、名字、班级、学号,每排每列对齐,没有对齐的地方用空格补齐。2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑,3.这个文件要有纵横结构,每横表示一个记录4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改
时间: 2024-03-11 14:44:39 浏览: 72
以下是一个简单的实现,其中学生信息存储在students.dat文件中,每个学生信息占据一行,每个字段用空格分隔,每个字段的最大宽度为MAX_NAME_LEN,例如姓名和名字的最大宽度为10个字符。代码中实现了插入、删除和修改学生信息的功能。
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NAME_LEN 10
#define MAX_CLASS_LEN 10
#define MAX_ID_LEN 10
#define MAX_LINE_LEN (MAX_NAME_LEN*2+MAX_CLASS_LEN+MAX_ID_LEN+3)
struct student {
char name[MAX_NAME_LEN];
char surname[MAX_NAME_LEN];
char class[MAX_CLASS_LEN];
char id[MAX_ID_LEN];
};
void print_student(struct student *s) {
printf("%-*s %-*s %-*s %s\n",
MAX_NAME_LEN, s->name,
MAX_NAME_LEN, s->surname,
MAX_CLASS_LEN, s->class,
s->id);
}
void print_header() {
printf("%-*s %-*s %-*s %s\n",
MAX_NAME_LEN, "姓名",
MAX_NAME_LEN, "名字",
MAX_CLASS_LEN, "班级",
"学号");
printf("%s\n", "===================================================");
}
void insert_student() {
char line[MAX_LINE_LEN];
struct student s;
printf("请输入学生信息(姓名 名字 班级 学号):");
fgets(line, sizeof(line), stdin);
sscanf(line, "%s %s %s %s", s.name, s.surname, s.class, s.id);
FILE *fp = fopen("students.dat", "a");
fprintf(fp, "%-*s %-*s %-*s %s\n",
MAX_NAME_LEN, s.name,
MAX_NAME_LEN, s.surname,
MAX_CLASS_LEN, s.class,
s.id);
fclose(fp);
}
void delete_student() {
char id[MAX_ID_LEN];
printf("请输入要删除的学生学号:");
scanf("%s", id);
getchar(); // 把换行符读掉
FILE *fp = fopen("students.dat", "r");
FILE *fp_tmp = fopen("students.tmp", "w");
char line[MAX_LINE_LEN];
int deleted = 0;
while (fgets(line, sizeof(line), fp)) {
char tmp_id[MAX_ID_LEN];
sscanf(line+MAX_NAME_LEN*2+MAX_CLASS_LEN+3, "%s", tmp_id);
if (strcmp(tmp_id, id) == 0) {
deleted = 1;
continue;
}
fputs(line, fp_tmp);
}
fclose(fp);
fclose(fp_tmp);
if (deleted) {
remove("students.dat");
rename("students.tmp", "students.dat");
printf("删除成功!\n");
} else {
remove("students.tmp");
printf("找不到该学生!\n");
}
}
void modify_student() {
char id[MAX_ID_LEN];
printf("请输入要修改的学生学号:");
scanf("%s", id);
getchar(); // 把换行符读掉
FILE *fp = fopen("students.dat", "r");
FILE *fp_tmp = fopen("students.tmp", "w");
char line[MAX_LINE_LEN];
int modified = 0;
while (fgets(line, sizeof(line), fp)) {
char tmp_id[MAX_ID_LEN];
sscanf(line+MAX_NAME_LEN*2+MAX_CLASS_LEN+3, "%s", tmp_id);
if (strcmp(tmp_id, id) == 0) {
modified = 1;
struct student s;
printf("请输入学生信息(姓名 名字 班级 学号):");
fgets(line, sizeof(line), stdin);
sscanf(line, "%s %s %s %s", s.name, s.surname, s.class, s.id);
fprintf(fp_tmp, "%-*s %-*s %-*s %s\n",
MAX_NAME_LEN, s.name,
MAX_NAME_LEN, s.surname,
MAX_CLASS_LEN, s.class,
s.id);
} else {
fputs(line, fp_tmp);
}
}
fclose(fp);
fclose(fp_tmp);
if (modified) {
remove("students.dat");
rename("students.tmp", "students.dat");
printf("修改成功!\n");
} else {
remove("students.tmp");
printf("找不到该学生!\n");
}
}
int main() {
while (1) {
print_header();
FILE *fp = fopen("students.dat", "r");
char line[MAX_LINE_LEN];
while (fgets(line, sizeof(line), fp)) {
struct student s;
sscanf(line, "%s %s %s %s", s.name, s.surname, s.class, s.id);
print_student(&s);
}
fclose(fp);
printf("\n请选择操作:\n");
printf("1. 插入学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 退出程序\n");
int choice;
scanf("%d", &choice);
getchar(); // 把换行符读掉
switch (choice) {
case 1:
insert_student();
break;
case 2:
delete_student();
break;
case 3:
modify_student();
break;
case 4:
return 0;
default:
printf("无效的选项!\n");
break;
}
}
return 0;
}
```
阅读全文