这几天学了文件处理相关的知识,这里用这个例子来结合理解链表和文件处理。
要求:程序运行起来,检测是否存在目标文件,如果存在,读取文件内容,并保存在链表中,再一 一输出,输出结束问是否要更新文件,支持数据修改,数据添加
(这里我用的学生成绩管理系统)。
如果不存在,手动输入,用链表保存到文件中。
实现思路:首先,我先假设这个文件的形式是以学号、姓名、成绩占据每行。
就是这样的:
1
zhangsan
98
2
lisi
90
那么核心就是把文件读取到链表,在以链表形式写入文件。
读取文件写入链表:
f1=fopen(p,"r");
//fscanf函数的作用就是一行行的读取文件,并把文件的值存放在想要存放的值
while(fscanf(f1, "%d%s%d\n", &num_student,name_student,&score_student)!=EOF)
{
new=(struct Student *)malloc(sizeof(struct Student));
new->name=(char *)malloc(128);
new->pnext=NULL;
new->num=num_student;
new->score=score_student;
strcpy(new->name,name_student);
tail->pnext=new;//尾插法
tail=new;
}
fclose(f1);
把链表写入文件:
void Deposit_file(struct Student *p,char *fd,int eo)
{
FILE *fp;
if(eo==1)//eo是检测文件是否存在
{
fp=fopen(fd,"w+");
}
else
{
fp=fopen(fd,"a+");
}
while(p!=NULL)
{
fprintf(fp,"%d\n",p->num);
fprintf(fp,"%s\n",p->name);
fprintf(fp,"%d\n",p->score);
p=p->pnext;
}
}
那么剩下的就简单了 。
下面就把完整代码发出来
(备注:我这个代码运行是基于我的假设文件形式来写的 ,主要是提供一个思路)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
struct Student
{
int num;
char *name;
int score;
struct Student *pnext;
};
void InitStu(struct Student *new,int i)
{
printf("请输入No %d 位学号\n", i+1);
scanf("%d",&(new->num));
printf("请输入No %d 位分数\n", i+1);
scanf("%d",&(new->score));
printf("请输入No %d 位姓名\n", i+1);
scanf("%s",new->name);
}
struct Student *InitLink()
{
int len;
int i;
struct Student *new;
struct Student *head;
struct Student *tail;
head=(struct Student *)malloc(sizeof(struct Student));
head->name=(char *)malloc(128);
head->pnext=NULL;
tail=head;
printf("请问您要输入几位学生信息:\n");
scanf("%d",&len);
for(i=0;i<len;i++)
{
new=(struct Student *)malloc(sizeof(struct Student));
new->name=(char *)malloc(128);
new->pnext=NULL;
InitStu(new,i);
tail->pnext=new;
tail=new;
if(i==0)
{
head=new;
}
}
return head;
}
int judge_file(char *p)
{
FILE *f1;
f1=fopen(p,"r+");
if(f1==NULL)
{
return 0;
}
fclose(f1);
return 1;
}
struct Student *Filetolink(char *p)
{
FILE *f1;
int num_student;
int score_student;
char name_student[20];
struct Student *head;
struct Student *tail;
struct Student *new;
head=(struct Student *)malloc(sizeof(struct Student));
head->name=(char *)malloc(128);
head->pnext=NULL;
tail=head;
/*head->num=0;
head->score=100;
strcpy(head->name,"lw");*/
f1=fopen(p,"r");
while(fscanf(f1, "%d%s%d\n", &num_student,name_student,&score_student)!=EOF)
{
new=(struct Student *)malloc(sizeof(struct Student));
new->name=(char *)malloc(128);
new->pnext=NULL;
new->num=num_student;
new->score=score_student;
strcpy(new->name,name_student);
tail->pnext=new;
tail=new;
}
fclose(f1);
head=head->pnext;
return head;
}
struct Student *Alter_link(struct Student **p,int s_num,int s_score)
{
struct Student *head=*p;
while(head!=NULL)
{
if(head->num==s_num)
{
head->score=s_score;
}
head=head->pnext;
}
return *p;
}
struct Student *Add_link(struct Student *p,int id,int mark,char mon[20])
{
struct Student *head=p;
struct Student *tmp;
tmp=(struct Student *)malloc(sizeof(struct Student));
tmp->name=(char *)malloc(20);
tmp->pnext=NULL;
tmp->score=mark;
tmp->num=id;
strcpy(tmp->name,mon);
while(head->pnext!=NULL)
{
head=head->pnext;
}
head->pnext=tmp;
return p;
}
int id_student(struct Student *p)
{
int id;
while(p!=NULL)
{
if(p->pnext==NULL)
{
id=p->num;
}
p=p->pnext;
}
return id;
}
void Deposit_file(struct Student *p,char *fd,int eo)
{
FILE *fp;
if(eo==1)
{
fp=fopen(fd,"w+");
}
else
{
fp=fopen(fd,"a+");
}
while(p!=NULL)
{
fprintf(fp,"%d\n",p->num);
fprintf(fp,"%s\n",p->name);
fprintf(fp,"%d\n",p->score);
p=p->pnext;
}
}
void printlink(struct Student *p)
{
while(p!=NULL)
{
printf("%d %s %d\n",p->num,p->name,p->score);
p=p->pnext;
}
}
int main(int argc, char *argv[])
{
int ver;
int s_num;
int s_score;
int a_score;
char a_name[20];
struct Student *stu;
struct Student *stu1;
if(judge_file(argv[1])==1)
{
printf("文件存在\n");
printf("是否更新数据?\n");
stu=Filetolink(argv[1]);
while(1)
{
printf("1-添加学生信息,2-修改学生信息,3-不更新,退出\n");
scanf("%d",&ver);
switch(ver)
{
case 1:
printf("请输入要添加的学生的姓名\n");
scanf("%s",a_name);
printf("请输入要添加的学生的成绩\n");
scanf("%d",&a_score);
stu=Add_link(stu,id_student(stu)+1,a_score,a_name);
Deposit_file(stu,argv[1],judge_file(argv[1]));
printlink(stu);
break;
case 2:
printf("输入想要修改的学生的学号\n");
scanf("%d",&s_num);
printf("请输入要修改的学生的成绩\n");
scanf("%d",&s_score);
stu=Alter_link(&stu,s_num,s_score);
Deposit_file(stu,argv[1],judge_file(argv[1]));
printlink(stu);
break;
case 3:
Deposit_file(stu,argv[1],judge_file(argv[1]));
printlink(stu);
exit(0);
break;
default:
printf("请输入1-3\n");
break;
}
}
}
else
{
stu=InitLink();
Deposit_file(stu,argv[1],judge_file(argv[1]));
printlink(stu);
}
return 0;
}