13.4
int main(int argc, char *argv[]) { char ch[20]; int i=0; FILE *fp; if((fp=fopen("test.txt","w"))==NULL) { printf("cannot open this file\n"); exit(0); } scanf("%s",ch); strupr(ch); while(ch[i]!='!') { fputc(ch[i++],fp); } fclose(fp); }
13.5
int main(int argc, char *argv[]) { FILE *fp; char str1[20],str2[20],str[20]; if((fp=fopen("t1.txt","r"))==NULL) { printf("Cannot open file A!"); exit(0); } fgets(str1,10,fp); if((fp=fopen("t2.txt","r"))==NULL) { printf("Cannot open file B!"); exit(0); } int i,j,n; char t; fgets(str2,20,fp); strcat(str1,str2); strcpy(str,str1); n=strlen(str); for(j=0;j<n-1;j++) for(i=0;i<n-1-j;i++) if(str[i]>str[i+1]) { t=str[i]; str [i] = str [i + 1]; str[i+1]=t; } if((fp=fopen("C.txt","w"))==NULL) { printf("Cannot open C file!"); exit(0); } fputs(str,fp); fputs("\n",fp); printf("%s\n",str); return 0; }
13.6-13.9
struct student//Define the structure { int num; char name[10]; int score[3]; float ave; }stu[5]; int main(int argc, char *argv[]) { int i,j,sum,t; FILE *fp; struct student p,temp,re[6]; for(i=0;i<5;i++)//Structure assignment { printf("Enter the %d record\n",i+1); scanf("%d,%s",&stu[i].num,stu[i].name); printf("Please enter the grades of three courses\n"); scanf("%d,%d,%d",&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); sum=0; sum=stu[i].score[0]+stu[i].score[1]+stu[i].score[2]; stu[i].ave=sum/3.0; } if((fp=fopen("stud.txt","w"))==NULL)//Write the file { printf("cannot open stud.txt\n"); exit(0); } for(i=0;i<5;i++) fwrite(&stu[i],sizeof(struct student),1,fp); fclose(fp); if((fp=fopen("stud.txt","r"))==NULL)//Read the file { printf("cannot open stud.txt\n"); exit(0); } for(i=0;i<5;i++) { fread(&stu[i],sizeof(struct student),1,fp); printf("%d,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave); } fclose(fp); printf("\n"); for(i=0;i<4;i++)//File sorting for(j=i+1;j<5;j++) if(stu[i].ave>stu[j].ave) { temp = stu [i]; stu [i] = stu [j]; stu[j]=temp; } if((fp=fopen("stu_sort.txt","w"))==NULL) { printf("cannot open stud.txt\n"); exit(0); } for(i=0;i<5;i++) fwrite(&stu[i],sizeof(struct student),1,fp); fclose(fp); if((fp=fopen("stu_sort.txt","r"))==NULL) { printf("cannot open stud.txt\n"); exit(0); } for(i=0;i<5;i++) { fread(&stu[i],sizeof(struct student),1,fp); printf("%d,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave); } fclose(fp); //insert file printf("Please enter insert student data:\n"); scanf("%d,%s",&p.num,p.name); printf("Please enter the grades of three courses\n"); scanf("%d,%d,%d",&p.score[0],&p.score[1],&p.score[2]); sum=0; sum=p.score[0]+p.score[1]+p.score[2]; p.ave=sum/3.0; if((fp=fopen("stu_sort.txt","r"))==NULL) { printf("cannot open stud.txt\n"); exit(0); } for(i=0;i<5;i++) { fread(&stu[i],sizeof(struct student),1,fp); if(stu[i].ave>p.ave) { t=i;break; } else t=5; } fclose(fp); fp=fopen("sdent.txt","a"); if(t==5) { for(i=0;i<5;i++) fwrite(&stu[i],sizeof(struct student),1,fp); fwrite(&p,sizeof(struct student),1,fp); } else if(t==0) { fwrite(&p,sizeof(struct student),1,fp); fwrite(&stu[i],sizeof(struct student),1,fp); } else { for(i=0;i<t;i++) fwrite(&stu[i],sizeof(struct student),1,fp); fwrite(&p,sizeof(struct student),1,fp); for(i=t;i<5;i++) fwrite(&stu[i],sizeof(struct student),1,fp); } fclose(fp); //read the inserted file if((fp=fopen("sdent.txt","r"))==NULL) { printf("cannot open stud.txt\n"); exit(0); } for(i=0;i<6;i++) { fread(&re[i],sizeof(struct student),1,fp); printf("%d,%s,%d,%d,%d,%6.2f\n",re[i].num,re[i].name,re[i].score[0],re[i].score[1],re[i].score[2],re[i].ave); } fclose(fp); }
13.10-13.11
struct employ//Define the structure { char name[10]; int num; char sex; int old; char locate[20]; int money; char health[6]; char school[20]; }stu[2]={{"zhang",1,'m',30,"yinchuan",8000,"good","master"},{"wang",2,'w',45,"henan",10000,"good","doctor"}}; struct stud//Define salary data structure { char name[10]; int money; }sdent[2]; int main(int argc, char *argv[]) { FILE *fp; int i; fp=fopen("employee.txt","w");//Write data to employee for(i=0;i<2;i++) fwrite(&stu[i],sizeof(struct employ),1,fp); fclose(fp); fp=fopen("employee.txt","r");//Read the data in the employee and assign a value to the employee salary structure for(i=0;i<2;i++) { fread(&stu[i],sizeof(struct employ),1,fp); strcpy(sdent[i].name,stu[i].name); sdent[i].money=stu[i].money; } fclose(fp); fp=fopen("gz.txt","w");//Write data to the employee salary file for(i=0;i<2;i++) fwrite(&sdent[i],sizeof(struct stud),1,fp); fclose(fp); fp=fopen("gz.txt","r");//Read the data in the employee salary file and verify whether it is written for(i=0;i<2;i++) { fread(&sdent[i],sizeof(struct stud),1,fp); printf("%s,%d\n",sdent[i].name,sdent[i].money); } fclose(fp); //delete employee salary fp=fopen("gz.txt","r");//Read the data in the employee salary file and verify whether it is written for(i=0;i<2;i++) { fread(&sdent[i],sizeof(struct stud),1,fp); // printf("%s,%d\n",sdent[i].name,sdent[i].money); } fclose(fp); fp=fopen("gz.txt","w");//Write data to the employee salary file for(i=0;i<1;i++) fwrite(&sdent[i],sizeof(struct stud),1,fp); fclose(fp); fp=fopen("gz.txt","r");//Read the data in the employee salary file and verify whether it is written for(i=0;i<2;i++) { fread(&sdent[i],sizeof(struct stud),1,fp); printf("%s,%d\n",sdent[i].name,sdent[i].money); } fclose(fp); }