请用C语言编写完整的程序,要求: 1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:姓名、地址、学号,每排每列对齐,没有对齐的地方用空格补齐。 2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑, 3.这个文件要有纵横结构,每横表示一个记录 4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改, 其中学生信息存储在students.dat文件中 能在文件中第一行显示姓名、地址、学号,并且修改后能够自动保存。printf打印的部分要用中文
时间: 2024-03-11 19:43:45 浏览: 74
赋值运算-c语言课件\\第2章 用C语言编写程序
很好,下面是一个示例程序,它可以实现您的需求。请将程序保存为students.c,并将学生信息存储在students.dat文件中。
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_ADDRESS_LENGTH 50
#define MAX_ID_LENGTH 10
#define MAX_RECORD_LENGTH (MAX_NAME_LENGTH + MAX_ADDRESS_LENGTH + MAX_ID_LENGTH + 2)
typedef struct {
char name[MAX_NAME_LENGTH];
char address[MAX_ADDRESS_LENGTH];
char id[MAX_ID_LENGTH];
} Student;
void print_header() {
printf("%-20s%-50s%-10s\n", "姓名", "地址", "学号");
}
void print_student(Student *student) {
printf("%-20s%-50s%-10s\n", student->name, student->address, student->id);
}
void read_student(FILE *fp, Student *student) {
fscanf(fp, "%s %s %s", student->name, student->address, student->id);
}
void write_student(FILE *fp, Student *student) {
fprintf(fp, "%-20s%-50s%-10s\n", student->name, student->address, student->id);
}
void add_student() {
FILE *fp = fopen("students.dat", "a");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
Student student;
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生地址:");
scanf("%s", student.address);
printf("请输入学生学号:");
scanf("%s", student.id);
write_student(fp, &student);
fclose(fp);
printf("添加成功!\n");
}
void delete_student() {
char id[MAX_ID_LENGTH];
printf("请输入要删除的学生学号:");
scanf("%s", id);
FILE *fp = fopen("students.dat", "r");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
FILE *temp = fopen("temp.dat", "w");
if (temp == NULL) {
printf("无法打开临时文件\n");
fclose(fp);
return;
}
Student student;
while (!feof(fp)) {
read_student(fp, &student);
if (strcmp(student.id, id) != 0) {
write_student(temp, &student);
}
}
fclose(fp);
fclose(temp);
if (remove("students.dat") != 0) {
printf("无法删除文件\n");
return;
}
if (rename("temp.dat", "students.dat") != 0) {
printf("无法重命名文件\n");
return;
}
printf("删除成功!\n");
}
void update_student() {
char id[MAX_ID_LENGTH];
printf("请输入要修改的学生学号:");
scanf("%s", id);
FILE *fp = fopen("students.dat", "r+");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
Student student;
while (!feof(fp)) {
long pos = ftell(fp);
read_student(fp, &student);
if (strcmp(student.id, id) == 0) {
printf("请输入修改后的学生姓名:");
scanf("%s", student.name);
printf("请输入修改后的学生地址:");
scanf("%s", student.address);
printf("请输入修改后的学生学号:");
scanf("%s", student.id);
fseek(fp, pos, SEEK_SET);
write_student(fp, &student);
fclose(fp);
printf("修改成功!\n");
return;
}
}
fclose(fp);
printf("未找到该学生信息\n");
}
void list_students() {
FILE *fp = fopen("students.dat", "r");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
print_header();
Student student;
while (!feof(fp)) {
read_student(fp, &student);
if (strlen(student.name) > 0) {
print_student(&student);
}
}
fclose(fp);
}
int main() {
int choice;
do {
printf("\n学生信息管理系统\n");
printf("1. 添加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 查看学生信息\n");
printf("0. 退出系统\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
update_student();
break;
case 4:
list_students();
break;
case 0:
printf("谢谢使用!\n");
break;
default:
printf("无效的操作,请重新选择\n");
break;
}
} while (choice != 0);
return 0;
}
```
注意,这只是一个示例程序,可能存在一些缺陷和不足之处。建议您在使用前仔细阅读代码,并进行测试和验证。
阅读全文