要求用C语言编写:1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:序号、姓名、班级、学号,每排每列对齐,没有对齐的地方用空格补齐。2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑,3.这个文件要有纵横结构,每横表示一个记录4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改
时间: 2024-03-18 10:40:30 浏览: 20
以下是一个示例程序,可以实现对带有序号、姓名、班级、学号的结构化文件进行插入、删除、修改等操作:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LEN 100
#define MAX_RECORDS 100
typedef struct record {
int id;
char name[MAX_LINE_LEN];
char class[MAX_LINE_LEN];
char student_id[MAX_LINE_LEN];
} Record;
Record records[MAX_RECORDS]; // 存储所有记录
int num_records; // 记录总数
// 读取文件中的记录到内存中
void read_records(char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Failed to open file %s\n", filename);
exit(1);
}
num_records = 0;
char line[MAX_LINE_LEN];
while (fgets(line, MAX_LINE_LEN, file) != NULL) {
Record record;
sscanf(line, "%d %s %s %s", &(record.id), record.name, record.class, record.student_id);
records[num_records] = record;
num_records++;
}
fclose(file);
}
// 将内存中的记录写入文件
void write_records(char *filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Failed to open file %s\n", filename);
exit(1);
}
for (int i = 0; i < num_records; i++) {
fprintf(file, "%-5d %-20s %-20s %-20s\n", records[i].id, records[i].name, records[i].class, records[i].student_id);
}
fclose(file);
}
// 根据记录序号查找对应的记录
Record *find_record_by_id(int id) {
for (int i = 0; i < num_records; i++) {
if (records[i].id == id) {
return &(records[i]);
}
}
return NULL;
}
// 插入新记录
void insert_record(int id, char *name, char *class, char *student_id) {
if (num_records >= MAX_RECORDS) {
printf("Too many records\n");
return;
}
for (int i = num_records - 1; i >= 0; i--) {
if (records[i].id >= id) {
records[i+1] = records[i];
} else {
break;
}
}
Record record;
record.id = id;
strcpy(record.name, name);
strcpy(record.class, class);
strcpy(record.student_id, student_id);
records[0] = record;
num_records++;
}
// 删除指定序号的记录
void delete_record(int id) {
for (int i = 0; i < num_records; i++) {
if (records[i].id == id) {
for (int j = i+1; j < num_records; j++) {
records[j-1] = records[j];
}
num_records--;
return;
}
}
}
// 修改指定序号的记录
void update_record(int id, char *name, char *class, char *student_id) {
Record *record = find_record_by_id(id);
if (record == NULL) {
printf("Record not found\n");
return;
}
strcpy(record->name, name);
strcpy(record->class, class);
strcpy(record->student_id, student_id);
}
// 显示所有记录
void show_records() {
printf("%-5s %-20s %-20s %-20s\n", "ID", "Name", "Class", "Student ID");
for (int i = 0; i < num_records; i++) {
printf("%-5d %-20s %-20s %-20s\n", records[i].id, records[i].name, records[i].class, records[i].student_id);
}
}
int main() {
read_records("records.txt");
while (1) {
printf("\n1. Insert record\n");
printf("2. Delete record\n");
printf("3. Update record\n");
printf("4. Show all records\n");
printf("5. Save and quit\n");
printf("Please choose an option: ");
int option;
scanf("%d", &option);
switch (option) {
case 1: {
int id;
char name[MAX_LINE_LEN];
char class[MAX_LINE_LEN];
char student_id[MAX_LINE_LEN];
printf("Please input record ID: ");
scanf("%d", &id);
printf("Please input record name: ");
scanf("%s", name);
printf("Please input record class: ");
scanf("%s", class);
printf("Please input record student ID: ");
scanf("%s", student_id);
insert_record(id, name, class, student_id);
break;
}
case 2: {
int id;
printf("Please input record ID: ");
scanf("%d", &id);
delete_record(id);
break;
}
case 3: {
int id;
char name[MAX_LINE_LEN];
char class[MAX_LINE_LEN];
char student_id[MAX_LINE_LEN];
printf("Please input record ID: ");
scanf("%d", &id);
printf("Please input new record name: ");
scanf("%s", name);
printf("Please input new record class: ");
scanf("%s", class);
printf("Please input new record student ID: ");
scanf("%s", student_id);
update_record(id, name, class, student_id);
break;
}
case 4: {
show_records();
break;
}
case 5: {
write_records("records.txt");
return 0;
}
default: {
printf("Invalid option\n");
break;
}
}
}
}
```
在运行程序之前,需要先创建一个名为 `records.txt` 的文本文件,将需要管理的记录按照格式写入其中。例如:
```
1 John Smith Class 1 10001
2 Alice Johnson Class 2 10002
3 Bob Brown Class 3 10003
```
运行程序后,可以按照提示进行插入、删除、修改和显示记录等操作。所有修改都会在内存中进行,只有在选择“Save and quit”选项后,才会将修改后的记录写入到文件中。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)