二进制文件排序、特殊项增减、结构体数据搜索 修改处理模版

#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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值