#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[100];
float score;
}stud;
int cmp(const void *a,const void *b)
{
if((*(struct student*)a).score==(*(struct student*)b).score)
return strcmp((*(struct student*)a).name,(*(struct student*)b).name);
return (*(struct student*)a).score-(*(struct student*)b).score;
}
int main(int argc, char const *argv[])
{
char str[80];
int ch=1;
FILE *fp;
if((fp=fopen("/Users/hellooks/Desktop/fileio/filerec.txt","wb"))==NULL)
{
printf("fail to open\n");
exit(0);
}
int size=1;
while(ch!=0){
printf("这是第%d次输入\n",size);
printf("输入name:\n");
gets(stud.name);
printf("继续输入score\n");
gets(str);
stud.score=atof(str);
fwrite(&stud,sizeof(stud),1,fp);
printf("继续输入?y/n\n");//注意输入方式 此处回车 需要用getchar()吸收
scanf("%d",&ch);
getchar();
size++;
}
fclose(fp);
printf("以下为读取\n");
FILE *fr;
if((fr=fopen("/Users/hellooks/Desktop/fileio/filerec.txt","r+"))==NULL)
{
printf("fail to open\n");
exit(0);
}
struct student aa[100];
int i=0;
while(fread(&stud,sizeof(stud),1,fp)==1)
{
printf("name:%s\n",stud.name);
printf("score:%f\n",stud.score);
strcpy(aa[i].name,stud.name);
aa[i].score=stud.score;
i++;
}
printf("over\n");
for(int i=0;i<size-1;i++)
printf("%s %f\n",aa[i].name,aa[i].score);
printf("\n");
qsort(aa,size-1,sizeof(aa[1]),cmp);
for(int i=0;i<size-1;i++)
printf("%s %f\n",aa[i].name,aa[i].score);
printf("按照分数 姓名排序结束\n");
//注意此时如果直接写入 不能覆盖到原来数据 因为位置指针在文末
//或者fseek(fr,0L,SEEK_SET);//位置指针放在SEEK_SET位置
rewind(fr);
for(int i=0;i<size-1;i++)
{
strcpy(stud.name,aa[i].name);
stud.score=aa[i].score;
fwrite(&stud,sizeof(stud),1,fr);
}
printf("排序后的数据重新写入\n");fclose(fr);
FILE *fq;
if((fq=fopen("/Users/hellooks/Desktop/fileio/filerec.txt","r+"))==NULL)
{
printf("fail to open\n");
exit(0);
}
while(fread(&stud,sizeof(stud),1,fq)==1)
{
printf("name:%s\n",stud.name);
printf("score:%f\n",stud.score);
}
int n;
fseek(fq,0,SEEK_SET);
while(fread(&stud,sizeof(stud),1,fq)==1)
{
if(strcmp(stud.name,"xks")==0)
{
stud.score+=100;
fseek(fq,-sizeof(stud),SEEK_CUR);//因为改动数据 此处必须返回记录的读写位置
fwrite(&stud,sizeof(stud),1,fq);
fseek(fp,0,SEEK_CUR);
}
}
printf("输入一个整数\n");
while(scanf("%d",&n)&&n!=0){
fseek(fq,(n-1)*sizeof(stud),SEEK_SET);
fread(&stud,sizeof(stud),1,fq);
printf("%s\n",stud.name);
printf("%f\n",stud.score);
}
printf("next\n");
FILE *fp_new;
if((fp=fopen("/Users/hellooks/Desktop/fileio/filerec.txt","r+b"))==NULL)
{
printf("fail to open!\n");
exit(0);
}
if((fp_new=fopen("/Users/hellooks/Desktop/fileio/new.txt","w+b"))==NULL)
{
printf("fail to create\n");
exit(0);
}
while(fread(&stud,sizeof(stud),1,fp)==1){
if(strcmp(stud.name,"xks")!=0)
{
//fseek(fp,-sizeof(stud),SEEK_CUR);//此处不需要 因为未改动数据
fwrite(&stud,sizeof(stud),1,fp_new);
printf("---------ok------\n");
}
}fclose(fp);
fclose(fp_new);
//remove("/Users/hellooks/Desktop/fileio/filerec.txt");
rename("/Users/hellooks/Desktop/fileio/new.txt","/Users/hellooks/Desktop/fileio/new2.txt");
if((fp_new=fopen("/Users/hellooks/Desktop/fileio/new2.txt","rb"))==NULL)
{
printf("fail to open\n");
exit(0);
}
while(fread(&stud,sizeof(stud),1,fp_new)==1)
{
printf("%s\n",stud.name);
printf("%f\n",stud.score );
}
return 0;
}