要求用C语言编写:1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:姓名、地址、学号,每排每列对齐,没有对齐的地方用空格补齐。2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑,3.这个文件要有纵横结构,每横表示一个记录4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改
时间: 2024-03-10 08:51:28 浏览: 248
好的,我可以帮你解决这个问题。以下是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_ADDRESS_LEN 50
#define MAX_ID_LEN 10
#define MAX_RECORDS 100
typedef struct {
char name[MAX_NAME_LEN];
char address[MAX_ADDRESS_LEN];
char id[MAX_ID_LEN];
} Record;
int read_records(Record records[], int *n, const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
return 0;
}
char buf[MAX_NAME_LEN + MAX_ADDRESS_LEN + MAX_ID_LEN + 3];
int i = 0;
while (fgets(buf, sizeof(buf), fp) != NULL) {
char *name = strtok(buf, " ");
char *address = strtok(NULL, " ");
char *id = strtok(NULL, " ");
if (name == NULL || address == NULL || id == NULL) {
fclose(fp);
return 0;
}
strcpy(records[i].name, name);
strcpy(records[i].address, address);
strcpy(records[i].id, id);
i++;
}
fclose(fp);
*n = i;
return 1;
}
void print_records(const Record records[], int n) {
printf("%-20s%-50s%-10s\n", "姓名", "地址", "学号");
for (int i = 0; i < n; i++) {
printf("%-20s%-50s%-10s\n", records[i].name, records[i].address, records[i].id);
}
}
int write_records(const Record records[], int n, const char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
return 0;
}
for (int i = 0; i < n; i++) {
fprintf(fp, "%-20s%-50s%-10s\n", records[i].name, records[i].address, records[i].id);
}
fclose(fp);
return 1;
}
int find_record(const Record records[], int n, const char *id) {
for (int i = 0; i < n; i++) {
if (strcmp(records[i].id, id) == 0) {
return i;
}
}
return -1;
}
void insert_record(Record records[], int *n) {
if (*n >= MAX_RECORDS) {
printf("记录已满,无法插入!\n");
return;
}
Record record;
printf("请输入姓名、地址、学号:");
scanf("%s %s %s", record.name, record.address, record.id);
records[*n] = record;
(*n)++;
}
void delete_record(Record records[], int *n) {
char id[MAX_ID_LEN];
printf("请输入要删除的学号:");
scanf("%s", id);
int pos = find_record(records, *n, id);
if (pos == -1) {
printf("找不到该学号的记录!\n");
return;
}
for (int i = pos; i < *n - 1; i++) {
records[i] = records[i + 1];
}
(*n)--;
}
void update_record(Record records[], int n) {
char id[MAX_ID_LEN];
printf("请输入要修改的学号:");
scanf("%s", id);
int pos = find_record(records, n, id);
if (pos == -1) {
printf("找不到该学号的记录!\n");
return;
}
Record record;
printf("请输入新的姓名、地址、学号:");
scanf("%s %s %s", record.name, record.address, record.id);
records[pos] = record;
}
int main() {
Record records[MAX_RECORDS];
int n = 0;
if (!read_records(records, &n, "records.txt")) {
printf("读取文件失败!\n");
return 1;
}
while (1) {
printf("请选择操作:\n");
printf("1. 显示记录\n");
printf("2. 插入记录\n");
printf("3. 删除记录\n");
printf("4. 修改记录\n");
printf("5. 保存并退出\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
print_records(records, n);
break;
case 2:
insert_record(records, &n);
break;
case 3:
delete_record(records, &n);
break;
case 4:
update_record(records, n);
break;
case 5:
if (!write_records(records, n, "records.txt")) {
printf("保存文件失败!\n");
return 1;
}
printf("已保存并退出。\n");
return 0;
default:
printf("无效操作!\n");
}
}
}
```
这个程序使用了一个 `Record` 结构体来存储每个记录的姓名、地址和学号,使用了文件读写来进行数据的存储和读取,使用了简单的插入、删除和修改操作来对记录进行更改。你可以根据需要进行修改和完善。
阅读全文