C语言 如何将文件里数据读入链表中,在下次用时自动导入数据
#include<stdio.h>#include<stdlib.h>#include<conio.h>#defineLENsizeof(structStu)struct...
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define LEN sizeof(struct Stu)
struct Stu *inlist();
void save(struct Stu *head);
struct Stu *add(struct Stu *head);
void insertNode(struct Stu *head,struct Stu *newNode);
struct Stu *read();
void print(struct Stu *head);
typedef struct Stu{
char name[20];
char sex[10];
char dep[20];
int number;
struct Stu *next;
}sqlist;
int main()
{
sqlist *head;
head=read();
getchar();
getchar();
head=inlist();
save(head);
head=add(head);
save(head);
read(head);
return 0;
}
sqlist *inlist()/*数据的输入*/
{
sqlist *p,*q;
sqlist *head;
int n=0;
printf("请输入名字 性别 部门 学号:\n");
p=q=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",&p->name,&p->sex,&p->dep,&p->number);
head=NULL;
while(p->number!=0)
{n=n+1;
if(n==1)
head=p;
else
q->next=p;
q=p;
p=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",&p->name,&p->sex,&p->dep,&p->number);
}
q->next=NULL;
return head;
}
void save(sqlist *head)
{
sqlist *p;
FILE *f;
p=head;
if(p!=NULL)
{
f=fopen("amd.txt","w+");
do
{
fprintf(f,"%5s%5s%5s%5d\n",&p->name,&p->sex,&p->dep,&p->number);
printf("%5s%5s%5s%5d\n",p->name,p->sex,p->dep,p->number);
p=p->next;
}
while(p!=NULL);
}
close(f);
}
sqlist *add(sqlist *head)
{
sqlist *p,*New;
//sqlist *q;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
printf("请输入名字 性别 部门 学号:\n");
New=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",&New->name,&New->sex,&New->dep,&New->number);
New->next=p->next;
p->next=New;
if(New->next==NULL)
{
return head;
}
else
printf("wait");
return head;
}
sqlist *read()
{
FILE *fp;
sqlist *f,*head;
if((fp=fopen("amd.txt","r+"))==NULL)
{
printf("打开失败!!");
}
else
printf("打开成功");
head=(sqlist*)malloc(LEN);
if(head==NULL)
{
printf("分配内存失败!\n");
exit(0);
}
head->next=NULL;
f=(sqlist*)malloc(LEN);
f->next=NULL;
if(fread(f,LEN,1,fp)==NULL)
{
printf("文件中没有数据!\n");
}
else{
insertNode(head,f);
while(!feof(fp))
{
f=(sqlist*)malloc(LEN);
f->next=NULL;
if(fread(f,LEN,1,fp)==1)
{
insertNode(head,f);
}
else
free(f);
}
}
fclose(fp);
return head;
/*do
{
//printf("*");
//fscanf(fp,"%s%s%s%d",&f->name,&f->sex,&f->dep,&f->number);
printf("%5s%5s%5s%5d\n",f->name,f->sex,f->dep,f->number);
f=f->next;
}while(fscanf(fp,"%s%s%s%d",&f->name,&f->sex,&f->dep,&f->number)!=EOF);
close(fp);*/
}
void insertNode(sqlist *head,sqlist *newNode)
{
sqlist *f;
f=head;
while(f->next!=NULL)
{
f=f->next;
}
f->next=newNode;
newNode->next=NULL;
}
void print(sqlist *head)
{
sqlist *f;
f=head;
if(f!=NULL)
{
do
{
printf("%5s%5s%5s%5d\n",f->name,f->sex,f->dep,f->number);
f=f->next;
}while(f!=NULL);
}
}
我已近实现了存储功能。。。。。。
希望高手指点啊!! 展开
#include<stdlib.h>
#include<conio.h>
#define LEN sizeof(struct Stu)
struct Stu *inlist();
void save(struct Stu *head);
struct Stu *add(struct Stu *head);
void insertNode(struct Stu *head,struct Stu *newNode);
struct Stu *read();
void print(struct Stu *head);
typedef struct Stu{
char name[20];
char sex[10];
char dep[20];
int number;
struct Stu *next;
}sqlist;
int main()
{
sqlist *head;
head=read();
getchar();
getchar();
head=inlist();
save(head);
head=add(head);
save(head);
read(head);
return 0;
}
sqlist *inlist()/*数据的输入*/
{
sqlist *p,*q;
sqlist *head;
int n=0;
printf("请输入名字 性别 部门 学号:\n");
p=q=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",&p->name,&p->sex,&p->dep,&p->number);
head=NULL;
while(p->number!=0)
{n=n+1;
if(n==1)
head=p;
else
q->next=p;
q=p;
p=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",&p->name,&p->sex,&p->dep,&p->number);
}
q->next=NULL;
return head;
}
void save(sqlist *head)
{
sqlist *p;
FILE *f;
p=head;
if(p!=NULL)
{
f=fopen("amd.txt","w+");
do
{
fprintf(f,"%5s%5s%5s%5d\n",&p->name,&p->sex,&p->dep,&p->number);
printf("%5s%5s%5s%5d\n",p->name,p->sex,p->dep,p->number);
p=p->next;
}
while(p!=NULL);
}
close(f);
}
sqlist *add(sqlist *head)
{
sqlist *p,*New;
//sqlist *q;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
printf("请输入名字 性别 部门 学号:\n");
New=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",&New->name,&New->sex,&New->dep,&New->number);
New->next=p->next;
p->next=New;
if(New->next==NULL)
{
return head;
}
else
printf("wait");
return head;
}
sqlist *read()
{
FILE *fp;
sqlist *f,*head;
if((fp=fopen("amd.txt","r+"))==NULL)
{
printf("打开失败!!");
}
else
printf("打开成功");
head=(sqlist*)malloc(LEN);
if(head==NULL)
{
printf("分配内存失败!\n");
exit(0);
}
head->next=NULL;
f=(sqlist*)malloc(LEN);
f->next=NULL;
if(fread(f,LEN,1,fp)==NULL)
{
printf("文件中没有数据!\n");
}
else{
insertNode(head,f);
while(!feof(fp))
{
f=(sqlist*)malloc(LEN);
f->next=NULL;
if(fread(f,LEN,1,fp)==1)
{
insertNode(head,f);
}
else
free(f);
}
}
fclose(fp);
return head;
/*do
{
//printf("*");
//fscanf(fp,"%s%s%s%d",&f->name,&f->sex,&f->dep,&f->number);
printf("%5s%5s%5s%5d\n",f->name,f->sex,f->dep,f->number);
f=f->next;
}while(fscanf(fp,"%s%s%s%d",&f->name,&f->sex,&f->dep,&f->number)!=EOF);
close(fp);*/
}
void insertNode(sqlist *head,sqlist *newNode)
{
sqlist *f;
f=head;
while(f->next!=NULL)
{
f=f->next;
}
f->next=newNode;
newNode->next=NULL;
}
void print(sqlist *head)
{
sqlist *f;
f=head;
if(f!=NULL)
{
do
{
printf("%5s%5s%5s%5d\n",f->name,f->sex,f->dep,f->number);
f=f->next;
}while(f!=NULL);
}
}
我已近实现了存储功能。。。。。。
希望高手指点啊!! 展开
展开全部
给你优化了一下,你试试,有问题再联系
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Stu)
struct Stu *inlist();
void save(struct Stu *head);
struct Stu *add(struct Stu *head);
void insertNode(struct Stu *head,struct Stu *newNode);
struct Stu *read();
void print(struct Stu *head);
typedef struct Stu{
char name[20];
char sex[10];
char dep[20];
int number;
struct Stu *next;
}sqlist;
sqlist *inlist()/*数据的输入*/
{
sqlist *p,*q;
sqlist *head;
int n=0;
printf("请输入名字 性别 部门 学号:\n");
p=q=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",p->name,p->sex,p->dep,&p->number);
head=NULL;
while(p->number!=0)
{n=n+1;
if(n==1)
head=p;
else
q->next=p;
q=p;
p=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",p->name,p->sex,p->dep,&p->number);
}
q->next=NULL;
return head;
}
void save(sqlist *head)
{
sqlist *p;
FILE *f;
p=head;
if(p!=NULL)
{
f=fopen("amd.txt","w+");
do
{
/*
char name[20];
char sex[10];
char dep[20];
int number;
fprintf(f,"%5s%5s%5s%5d\n",&p->name,&p->sex,&p->dep,&p->number);
printf("%5s%5s%5s%5d\n",p->name,p->sex,p->dep,p->number);
数据如果超过5的长度,读的时候就不能正确取出来了,因此,应该按数据定义的长度来记录数据
+1是为了读取时方便,左对齐右补空格
**/
fprintf(f,"%-21.21s%-11.11s%-21.21s%-11d\n",p->name,p->sex,p->dep,p->number);
p=p->next;
}
while(p!=NULL);
fclose(f); //fclose要和fopen相呼应
}
//close(f);
}
sqlist *add(sqlist *head)
{
sqlist *p,*New;
//sqlist *q;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
printf("请输入名字 性别 部门 学号:\n");
New=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",New->name,New->sex,New->dep,&New->number);
New->next=p->next;
p->next=New;
if(New->next==NULL)
{
return head;
}
else
printf("wait");
return head;
}
sqlist *read()
{
FILE *fp;
sqlist *f,*head=NULL; //指针要习惯给它赋初值NULL,不要形成野指针
char str[128];
if((fp=fopen("amd.txt","r+"))==NULL)
{
printf("打开文件失败!!\n");
return head; //此时head is NULL
}
if( fgets( str , sizeof(str) , fp ) == NULL )
{
printf("文件中没有数据!\n");
fclose(fp);
return head; //此时head is NULL
}
//有数据再创建链表,不要创建空表
head=(sqlist*)malloc(LEN);
if(head==NULL)
{
printf("分配内存失败!\n");
fclose(fp);
exit(0);
}
head->next=NULL;
sscanf(str,"%s %s %s %d",head->name,head->sex,head->dep,&head->number) ;
while (fgets( str , sizeof(str) , fp ) != NULL)
{
f=(sqlist*)malloc(LEN);
f->next=NULL;
sscanf(str,"%s %s %s %d",f->name,f->sex,f->dep,&f->number) ;
insertNode(head,f);
}
fclose(fp);
return head;
}
void insertNode(sqlist *head,sqlist *newNode)
{
sqlist *f;
f=head;
while(f->next!=NULL)
{
f=f->next;
}
f->next=newNode;
newNode->next=NULL;
}
void print(sqlist *head)
{
sqlist *f;
f=head;
if(f!=NULL)
{
do
{
printf("%-21s%-11s%-21s%-11d\n",f->name,f->sex,f->dep,f->number);
f=f->next;
}while(f!=NULL);
}
}
int main()
{
sqlist *head;
head=read();
if ( head == NULL )
{
head=inlist();
save(head);
}
else
print(head);
head=add(head);
save(head);
print(head);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Stu)
struct Stu *inlist();
void save(struct Stu *head);
struct Stu *add(struct Stu *head);
void insertNode(struct Stu *head,struct Stu *newNode);
struct Stu *read();
void print(struct Stu *head);
typedef struct Stu{
char name[20];
char sex[10];
char dep[20];
int number;
struct Stu *next;
}sqlist;
sqlist *inlist()/*数据的输入*/
{
sqlist *p,*q;
sqlist *head;
int n=0;
printf("请输入名字 性别 部门 学号:\n");
p=q=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",p->name,p->sex,p->dep,&p->number);
head=NULL;
while(p->number!=0)
{n=n+1;
if(n==1)
head=p;
else
q->next=p;
q=p;
p=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",p->name,p->sex,p->dep,&p->number);
}
q->next=NULL;
return head;
}
void save(sqlist *head)
{
sqlist *p;
FILE *f;
p=head;
if(p!=NULL)
{
f=fopen("amd.txt","w+");
do
{
/*
char name[20];
char sex[10];
char dep[20];
int number;
fprintf(f,"%5s%5s%5s%5d\n",&p->name,&p->sex,&p->dep,&p->number);
printf("%5s%5s%5s%5d\n",p->name,p->sex,p->dep,p->number);
数据如果超过5的长度,读的时候就不能正确取出来了,因此,应该按数据定义的长度来记录数据
+1是为了读取时方便,左对齐右补空格
**/
fprintf(f,"%-21.21s%-11.11s%-21.21s%-11d\n",p->name,p->sex,p->dep,p->number);
p=p->next;
}
while(p!=NULL);
fclose(f); //fclose要和fopen相呼应
}
//close(f);
}
sqlist *add(sqlist *head)
{
sqlist *p,*New;
//sqlist *q;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
printf("请输入名字 性别 部门 学号:\n");
New=(sqlist*)malloc(LEN);
scanf("%s%s%s%d",New->name,New->sex,New->dep,&New->number);
New->next=p->next;
p->next=New;
if(New->next==NULL)
{
return head;
}
else
printf("wait");
return head;
}
sqlist *read()
{
FILE *fp;
sqlist *f,*head=NULL; //指针要习惯给它赋初值NULL,不要形成野指针
char str[128];
if((fp=fopen("amd.txt","r+"))==NULL)
{
printf("打开文件失败!!\n");
return head; //此时head is NULL
}
if( fgets( str , sizeof(str) , fp ) == NULL )
{
printf("文件中没有数据!\n");
fclose(fp);
return head; //此时head is NULL
}
//有数据再创建链表,不要创建空表
head=(sqlist*)malloc(LEN);
if(head==NULL)
{
printf("分配内存失败!\n");
fclose(fp);
exit(0);
}
head->next=NULL;
sscanf(str,"%s %s %s %d",head->name,head->sex,head->dep,&head->number) ;
while (fgets( str , sizeof(str) , fp ) != NULL)
{
f=(sqlist*)malloc(LEN);
f->next=NULL;
sscanf(str,"%s %s %s %d",f->name,f->sex,f->dep,&f->number) ;
insertNode(head,f);
}
fclose(fp);
return head;
}
void insertNode(sqlist *head,sqlist *newNode)
{
sqlist *f;
f=head;
while(f->next!=NULL)
{
f=f->next;
}
f->next=newNode;
newNode->next=NULL;
}
void print(sqlist *head)
{
sqlist *f;
f=head;
if(f!=NULL)
{
do
{
printf("%-21s%-11s%-21s%-11d\n",f->name,f->sex,f->dep,f->number);
f=f->next;
}while(f!=NULL);
}
}
int main()
{
sqlist *head;
head=read();
if ( head == NULL )
{
head=inlist();
save(head);
}
else
print(head);
head=add(head);
save(head);
print(head);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询