要求用C语言编写:1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:序号、姓名、班级、学号,每排每列对齐,没有对齐的地方用空格补齐。2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑,3.这个文件要有纵横结构,每横表示一个记录4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改
时间: 2024-03-18 09:39:33 浏览: 78
好的,根据您的需求,我为您编写了以下C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_CLASS_LEN 20
#define MAX_ID_LEN 10
#define MAX_RECORDS 100
// 定义学生结构体
struct Student {
int id;
char name[MAX_NAME_LEN];
char class[MAX_CLASS_LEN];
char sid[MAX_ID_LEN];
};
// 定义记录结构体
struct Record {
int id;
char name[MAX_NAME_LEN];
char class[MAX_CLASS_LEN];
char sid[MAX_ID_LEN];
};
// 定义全局变量
struct Record records[MAX_RECORDS]; // 存储所有记录
int num_records; // 记录总数
// 函数声明
void read_file();
void save_file();
void print_records();
void add_record();
void delete_record();
void modify_record();
int main() {
read_file();
print_records();
int choice;
do {
printf("请选择操作:\n");
printf("1. 添加记录\n");
printf("2. 删除记录\n");
printf("3. 修改记录\n");
printf("4. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_record();
break;
case 2:
delete_record();
break;
case 3:
modify_record();
break;
case 4:
break;
default:
printf("无效的选择,请重新输入!\n");
break;
}
} while (choice != 4);
save_file();
return 0;
}
// 从文件中读取记录
void read_file() {
FILE *fp = fopen("students.dat", "rb");
if (fp == NULL) {
printf("无法打开文件!\n");
exit(1);
}
fseek(fp, 0, SEEK_END);
int file_size = ftell(fp);
num_records = file_size / sizeof(struct Student);
fseek(fp, 0, SEEK_SET);
for (int i = 0; i < num_records; i++) {
struct Student student;
fread(&student, sizeof(struct Student), 1, fp);
struct Record record;
record.id = i + 1;
strcpy(record.name, student.name);
strcpy(record.class, student.class);
strcpy(record.sid, student.sid);
records[i] = record;
}
fclose(fp);
}
// 将记录保存到文件中
void save_file() {
FILE *fp = fopen("students.dat", "wb");
if (fp == NULL) {
printf("无法打开文件!\n");
exit(1);
}
for (int i = 0; i < num_records; i++) {
struct Student student;
strcpy(student.name, records[i].name);
strcpy(student.class, records[i].class);
strcpy(student.sid, records[i].sid);
fwrite(&student, sizeof(struct Student), 1, fp);
}
fclose(fp);
}
// 打印所有记录
void print_records() {
printf("序号\t姓名\t班级\t学号\n");
for (int i = 0; i < num_records; i++) {
printf("%d\t%s\t%s\t%s\n", records[i].id, records[i].name, records[i].class, records[i].sid);
}
}
// 添加一条记录
void add_record() {
if (num_records >= MAX_RECORDS) {
printf("记录已满,无法添加!\n");
return;
}
printf("请输入姓名:");
char name[MAX_NAME_LEN];
scanf("%s", name);
printf("请输入班级:");
char class[MAX_CLASS_LEN];
scanf("%s", class);
printf("请输入学号:");
char sid[MAX_ID_LEN];
scanf("%s", sid);
struct Record record;
record.id = num_records + 1;
strcpy(record.name, name);
strcpy(record.class, class);
strcpy(record.sid, sid);
records[num_records] = record;
num_records++;
}
// 删除一条记录
void delete_record() {
if (num_records == 0) {
printf("记录为空,无法删除!\n");
return;
}
printf("请输入要删除的记录的序号:");
int id;
scanf("%d", &id);
if (id <= 0 || id > num_records) {
printf("无效的序号!\n");
return;
}
for (int i = id; i < num_records; i++) {
records[i - 1] = records[i];
records[i - 1].id = i;
}
num_records--;
}
// 修改一条记录
void modify_record() {
if (num_records == 0) {
printf("记录为空,无法修改!\n");
return;
}
printf("请输入要修改的记录的序号:");
int id;
scanf("%d", &id);
if (id <= 0 || id > num_records) {
printf("无效的序号!\n");
return;
}
printf("请输入姓名:");
char name[MAX_NAME_LEN];
scanf("%s", name);
printf("请输入班级:");
char class[MAX_CLASS_LEN];
scanf("%s", class);
printf("请输入学号:");
char sid[MAX_ID_LEN];
scanf("%s", sid);
struct Record record;
record.id = id;
strcpy(record.name, name);
strcpy(record.class, class);
strcpy(record.sid, sid);
records[id - 1] = record;
}
```
这个程序实现了从文件中读取记录、添加记录、删除记录、修改记录、保存记录到文件的功能,您可以根据自己的需要进行修改和完善。
阅读全文