要求用C语言编写:1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:序号、姓名、学号、地址,每排每列对齐,没有对齐的地方用空格补齐。2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑,3.这个文件要有纵横结构,每横表示一个记录4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改
时间: 2024-03-11 20:43:58 浏览: 55
南开一百例.docx
好的,我来简单介绍一下这个问题的解决思路:
1. 首先,需要定义一个结构体来表示每个记录的信息,包括序号、姓名、学号、地址等字段;
2. 然后,需要编写读取文件的函数,可以使用C标准库中的文件操作函数,比如fopen、fread等;
3. 接着,可以使用链表或数组等数据结构来存储文件中的记录信息,并实现插入、删除和修改等操作;
4. 最后,需要编写写入文件的函数,将修改后的记录重新写入文件中。
下面是一个简单的实现示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
//定义记录结构体
typedef struct Record{
int id;
char name[MAX_LEN];
char number[MAX_LEN];
char address[MAX_LEN];
struct Record *next;
} Record;
//读取文件中的记录
Record* readRecords(char* filename){
FILE *fp = fopen(filename, "r");
if(fp == NULL){
printf("Failed to open file %s", filename);
return NULL;
}
Record *head = NULL, *tail = NULL;
char line[MAX_LEN];
fgets(line, MAX_LEN, fp);//读取第一行,但不做处理
while(fgets(line, MAX_LEN, fp) != NULL){
Record *record = (Record*)malloc(sizeof(Record));
sscanf(line, "%d%s%s%s", &(record->id), record->name, record->number, record->address);
record->next = NULL;
if(head == NULL){
head = tail = record;
}else{
tail->next = record;
tail = record;
}
}
fclose(fp);
return head;
}
//在链表中添加新记录
Record* addRecord(Record *head, Record *newRecord){
if(head == NULL){
return newRecord;
}
Record *p = head;
while(p->next != NULL){
p = p->next;
}
p->next = newRecord;
return head;
}
//从链表中删除某个记录
Record* deleteRecord(Record *head, int id){
if(head == NULL){
return NULL;
}
Record *p = head, *pre = NULL;
while(p != NULL){
if(p->id == id){
if(pre == NULL){
head = p->next;
}else{
pre->next = p->next;
}
free(p);
return head;
}
pre = p;
p = p->next;
}
return head;
}
//修改链表中某个记录的信息
Record* modifyRecord(Record *head, int id, Record *newRecord){
if(head == NULL){
return NULL;
}
Record *p = head;
while(p != NULL){
if(p->id == id){
strcpy(p->name, newRecord->name);
strcpy(p->number, newRecord->number);
strcpy(p->address, newRecord->address);
return head;
}
p = p->next;
}
return head;
}
//将链表中的记录写入文件
void writeRecords(char *filename, Record *head){
FILE *fp = fopen(filename, "w");
if(fp == NULL){
printf("Failed to open file %s", filename);
return;
}
fprintf(fp, "%-10s%-20s%-20s%-20s\n", "序号", "姓名", "学号", "地址");//写入第一行
Record *p = head;
while(p != NULL){
fprintf(fp, "%-10d%-20s%-20s%-20s\n", p->id, p->name, p->number, p->address);
p = p->next;
}
fclose(fp);
}
//打印链表中的记录
void printRecords(Record *head){
printf("%-10s%-20s%-20s%-20s\n", "序号", "姓名", "学号", "地址");//打印第一行
Record *p = head;
while(p != NULL){
printf("%-10d%-20s%-20s%-20s\n", p->id, p->name, p->number, p->address);
p = p->next;
}
}
int main(){
Record *head = readRecords("records.txt");
if(head == NULL){
return 0;
}
printRecords(head);
//添加新记录
Record *newRecord = (Record*)malloc(sizeof(Record));
newRecord->id = 4;
strcpy(newRecord->name, "张三");
strcpy(newRecord->number, "20210004");
strcpy(newRecord->address, "北京市海淀区");
head = addRecord(head, newRecord);
printf("After adding new record:\n");
printRecords(head);
//删除记录
head = deleteRecord(head, 2);
printf("After deleting record with id=2:\n");
printRecords(head);
//修改记录
Record *modifiedRecord = (Record*)malloc(sizeof(Record));
modifiedRecord->id = 1;
strcpy(modifiedRecord->name, "王五");
strcpy(modifiedRecord->number, "20210001");
strcpy(modifiedRecord->address, "上海市浦东新区");
head = modifyRecord(head, 1, modifiedRecord);
printf("After modifying record with id=1:\n");
printRecords(head);
//将修改后的记录写入文件
writeRecords("records.txt", head);
return 0;
}
```
在这个示例中,我们使用了链表来存储记录信息,并实现了添加、删除和修改等操作。同时,我们还编写了读取和写入文件的函数,可以将记录信息存储在文件中,并在需要时重新读取和修改。
阅读全文