![]() |
#2
吹水佬2020-04-15 14:08
回复 楼主 cabage
|

#include<stdlib.h>
#include<string.h>
struct stulink{
int num;
char name[10];
char sex;
int score;
char rank;
int level;
stulink *next;
};
void save(stulink* head)
{
stulink *p1 = head;
FILE *fp1;
fp1 = fopen("e:\\student1.txt","w");
while(p1!=NULL){
fprintf(fp1,"%d %s %c %d %c",p1->num,p1->name,p1->sex,p1->score,p1->rank);
p1 = p1->next;
}
if(p1 == NULL)
printf("所有数据保存成功!!!");
fclose(fp1);
}
stulink *ReadFromFile()
{
stulink *head,*temp,*end;
head = (stulink *)malloc(sizeof(stulink *));
end = head;
FILE *fp1;
fp1 = fopen("e:\\student.txt","r");
int count = 1;
while(!feof(fp1)){
temp = (stulink *)malloc(sizeof(stulink *));
if(temp == NULL)printf("内存分配失败!!!");
fscanf(fp1,"%s %c %d %c\n",temp->name,&temp->sex,&temp->score,&temp->rank);
temp->num = count;
count++;
end->next = temp;
end = temp;
}
end->next = NULL;
fclose(fp1);
printf("创建成功!!!\n");
save(head);
return head;
}
void main()
{
ReadFromFile();
getchar();
}
#include<string.h>
struct stulink{
int num;
char name[10];
char sex;
int score;
char rank;
int level;
stulink *next;
};
void save(stulink* head)
{
stulink *p1 = head;
FILE *fp1;
fp1 = fopen("e:\\student1.txt","w");
while(p1!=NULL){
fprintf(fp1,"%d %s %c %d %c",p1->num,p1->name,p1->sex,p1->score,p1->rank);
p1 = p1->next;
}
if(p1 == NULL)
printf("所有数据保存成功!!!");
fclose(fp1);
}
stulink *ReadFromFile()
{
stulink *head,*temp,*end;
head = (stulink *)malloc(sizeof(stulink *));
end = head;
FILE *fp1;
fp1 = fopen("e:\\student.txt","r");
int count = 1;
while(!feof(fp1)){
temp = (stulink *)malloc(sizeof(stulink *));
if(temp == NULL)printf("内存分配失败!!!");
fscanf(fp1,"%s %c %d %c\n",temp->name,&temp->sex,&temp->score,&temp->rank);
temp->num = count;
count++;
end->next = temp;
end = temp;
}
end->next = NULL;
fclose(fp1);
printf("创建成功!!!\n");
save(head);
return head;
}
void main()
{
ReadFromFile();
getchar();
}