Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- typedef struct talmid{
- int code;
- double age;
- }std1;
- void bulidStudentFile(char *s);
- void printstudentFile(char *s);
- double AvgAge(char *s);
- void SortFile(char *s, char *nf);
- void main()
- {
- int run;
- char fname[30]="nbinf.bin";
- char sortfname[30]="nbinf.bin";
- double avg;
- bulidStudentFile(fname);
- printstudentFile(fname);
- avg=AvgAge(fname);
- printf("avg=%lf", avg);
- SortFile(fname, sortfname);
- printstudentFile(sortfname);
- scanf("%d", &run);
- }
- void bulidStudentFile(char *s)
- {
- int i, n;
- std1 talmid;
- FILE *fp;
- if((fp=fopen(s, "wb"))!=NULL)
- {
- printf("enter num of students \n");
- scanf("%d", &n);
- for(i=0; i<n ; i++)
- {
- printf("\n enter code and age \n");
- scanf("%d", &(talmid.code));
- scanf("%lf", &(talmid.age));
- fwrite(&talmid, sizeof(std1), 1, fp);
- }
- fclose(fp);
- }
- }
- void printstudentFile(char *s)
- {
- FILE *fp;
- std1 talmid;
- if((fp=fopen(s, "rb"))!=NULL)
- {
- fread(&talmid, sizeof(std1), 1, fp);
- while(!feof(fp))
- {
- printf("\n code=%d \n", talmid.code);
- printf("age=%lf \n", talmid.age);
- fread(&talmid, sizeof(std1), 1, fp);
- }
- fclose(fp);
- }
- }
- double AvgAge(char *s)
- {
- double avg=0;
- int count=0;
- FILE *fp;
- std1 talmid;
- if((fp=fopen(s, "rb"))!=NULL)
- {
- fread(&talmid, sizeof(std1), 1, fp);
- while(!feof(fp))
- {
- avg+=talmid.age;
- count++;
- fread(&talmid, sizeof(std1), 1, fp);
- }
- fclose(fp);
- }
- avg=avg/count;
- return avg;
- }
- void SortFile(char *s, char *nf)
- {
- FILE *fp;
- FILE *fp2;
- FILE *fp3;
- std1 talmid;
- int min;
- if((fp3=fopen(nf, "wb"))!=NULL)
- {
- while(!feof(fp3))
- {
- fp=fopen(s, "rb");
- fread(&talmid, sizeof(std1), 1, fp);
- min=talmid.code;
- fp2=fp;
- if((fp=fopen(s, "rb"))!=NULL)
- {
- fread(&talmid, sizeof(std1), 1, fp);
- while(!feof(fp))
- {
- if(min>talmid.code)
- {
- min=talmid.code;
- fp2=fp;
- }
- fread(&talmid, sizeof(std1), 1, fp);
- }
- talmid.code=-1;
- fwrite(&talmid, sizeof(std1), 1, fp2);
- fclose(fp);
- }
- talmid.code=min;
- fwrite(&talmid, sizeof(std1), 1, fp3);
- }
- fclose(fp3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement