要求用C语言编写:1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:序号、姓名、班级、学号,每排每列对齐,没有对齐的地方用空格补齐。2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑,3.这个文件要有纵横结构,每横表示一个记录4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改
时间: 2024-03-18 16:39:15 浏览: 40
以下是一个简单的实现,用于演示如何实现上述要求。请注意,这只是一个示例,实际应用中可能需要进行更多的错误检查和边界情况处理。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_CLASS_LEN 10
#define MAX_ID_LEN 20
typedef struct {
int id;
char name[MAX_NAME_LEN];
char class[MAX_CLASS_LEN];
char student_id[MAX_ID_LEN];
} Record;
void print_record(const Record* record) {
printf("%-10d%-20s%-10s%-20s\n", record->id, record->name, record->class, record->student_id);
}
void print_header() {
printf("%-10s%-20s%-10s%-20s\n", "序号", "姓名", "班级", "学号");
}
void read_records(const char* filename, Record** records_ptr, int* num_records_ptr) {
FILE* fp;
int num_records = 0;
Record* records = NULL;
char line[100];
char* token;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s for reading.\n", filename);
return;
}
fgets(line, sizeof(line), fp); // skip header
while (fgets(line, sizeof(line), fp)) {
records = realloc(records, (num_records + 1) * sizeof(Record));
token = strtok(line, " ");
records[num_records].id = atoi(token);
token = strtok(NULL, " ");
strncpy(records[num_records].name, token, MAX_NAME_LEN);
records[num_records].name[MAX_NAME_LEN - 1] = '\0';
token = strtok(NULL, " ");
strncpy(records[num_records].class, token, MAX_CLASS_LEN);
records[num_records].class[MAX_CLASS_LEN - 1] = '\0';
token = strtok(NULL, " ");
strncpy(records[num_records].student_id, token, MAX_ID_LEN);
records[num_records].student_id[MAX_ID_LEN - 1] = '\0';
num_records++;
}
fclose(fp);
*records_ptr = records;
*num_records_ptr = num_records;
}
void write_records(const char* filename, const Record* records, int num_records) {
FILE* fp;
int i;
fp = fopen(filename, "w");
if (fp == NULL) {
printf("Failed to open file %s for writing.\n", filename);
return;
}
print_header();
for (i = 0; i < num_records; i++) {
print_record(&records[i]);
fprintf(fp, "%d %s %s %s\n", records[i].id, records[i].name, records[i].class, records[i].student_id);
}
fclose(fp);
}
void add_record(Record** records_ptr, int* num_records_ptr) {
Record* records = *records_ptr;
int num_records = *num_records_ptr;
Record record;
char buffer[100];
printf("请输入新记录的信息:\n");
printf("序号:");
fgets(buffer, sizeof(buffer), stdin);
record.id = atoi(buffer);
printf("姓名:");
fgets(record.name, MAX_NAME_LEN, stdin);
record.name[strlen(record.name) - 1] = '\0';
printf("班级:");
fgets(record.class, MAX_CLASS_LEN, stdin);
record.class[strlen(record.class) - 1] = '\0';
printf("学号:");
fgets(record.student_id, MAX_ID_LEN, stdin);
record.student_id[strlen(record.student_id) - 1] = '\0';
records = realloc(records, (num_records + 1) * sizeof(Record));
records[num_records] = record;
*records_ptr = records;
*num_records_ptr = num_records + 1;
}
void delete_record(Record** records_ptr, int* num_records_ptr) {
Record* records = *records_ptr;
int num_records = *num_records_ptr;
int id;
int i, j;
printf("请输入要删除的记录的序号:");
scanf("%d", &id);
for (i = 0; i < num_records; i++) {
if (records[i].id == id) {
for (j = i; j < num_records - 1; j++) {
records[j] = records[j + 1];
}
*records_ptr = realloc(records, (num_records - 1) * sizeof(Record));
*num_records_ptr = num_records - 1;
printf("已删除记录:\n");
print_header();
print_record(&records[i]);
return;
}
}
printf("未找到序号为 %d 的记录。\n", id);
}
void modify_record(Record* records, int num_records) {
int id;
int i;
printf("请输入要修改的记录的序号:");
scanf("%d", &id);
for (i = 0; i < num_records; i++) {
if (records[i].id == id) {
printf("请输入修改后的信息:\n");
printf("姓名:");
fgets(records[i].name, MAX_NAME_LEN, stdin);
records[i].name[strlen(records[i].name) - 1] = '\0';
printf("班级:");
fgets(records[i].class, MAX_CLASS_LEN, stdin);
records[i].class[strlen(records[i].class) - 1] = '\0';
printf("学号:");
fgets(records[i].student_id, MAX_ID_LEN, stdin);
records[i].student_id[strlen(records[i].student_id) - 1] = '\0';
printf("已修改记录为:\n");
print_header();
print_record(&records[i]);
return;
}
}
printf("未找到序号为 %d 的记录。\n", id);
}
int main() {
char filename[] = "records.txt";
Record* records = NULL;
int num_records = 0;
int option;
char buffer[100];
read_records(filename, &records, &num_records);
do {
printf("\n请选择要进行的操作:\n");
printf("1. 显示所有记录\n");
printf("2. 添加记录\n");
printf("3. 删除记录\n");
printf("4. 修改记录\n");
printf("5. 保存并退出\n");
printf("请选择:");
fgets(buffer, sizeof(buffer), stdin);
option = atoi(buffer);
switch (option) {
case 1:
print_header();
for (int i = 0; i < num_records; i++) {
print_record(&records[i]);
}
break;
case 2:
add_record(&records, &num_records);
break;
case 3:
delete_record(&records, &num_records);
break;
case 4:
modify_record(records, num_records);
break;
case 5:
write_records(filename, records, num_records);
break;
default:
printf("无效的选项,请重新选择。\n");
}
} while (option != 5);
free(records);
return 0;
}
```
阅读全文