(1)从键盘输入一个文件名,以及一个以#结束的字符序列,将输入的字符保存到文件中去。
#include "stdio.h" #include "stdlib.h" int main() { FILE *fp ; char ch,fname[10]; printf("文件名:"); gets(fname); if ((fp=fopen(fname,"w"))==NULL) { printf("connot open\n"); exit(0); } while ((ch=getchar())!='#') fputc(ch,fp); fclose(fp); return 0; }
#include<stdlib.h> #include<stdio.h> int main() { FILE *fp1,*fp2; char c; if((fp1=fopen("f1.dat","r"))==NULL) { printf("connot open\n"); exit(0); } if((fp2=fopen("f2.dat","w"))==NULL) { printf("connot open\n"); exit(0); } c=fgetc(fp1); while(c!=EOF) { fputc(c,fp2); c=fgetc(fp1); } fclose(fp1); fclose(fp2); return 0; }
#include<stdio.h> #include<stdlib.h> int main() { FILE *fp1,*fp2; char ch; fp1=fopen("file1.dat","r"); fp2=fopen("file2.dat","w"); while(!feof(fp1)) { ch=fgetc(fp1); putchar(ch); fputc(ch,fp2); } fclose(fp1); fclose(fp2); printf("\n"); return 0; }运行结果:
#include<stdio.h> #include<stdlib.h> int main() { FILE *fp; char ch; int num=0,sp=0,wor=0,n=0; fp=fopen("test.txt","r"); if(fp==NULL) { printf("connot open\n"); exit(0); } while((ch=fgetc(fp))!=EOF) { n++; if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) wor++; else if(ch>='0'&&ch<='9') num++; else if(ch==' ') sp++; } printf("字母%d,数字%d,空格%d,字节%d\n",wor,num,sp,n); fclose(fp); return 0; }运行结果:
#include<stdio.h> #include<stdlib.h> int main() { int score; int excelent=0,fail=0,count=0; double sum=0,ave; FILE *fp; fp=fopen("english.dat","r"); if(fp==NULL) { printf("open error!\n"); exit(1); } while(fscanf(fp,"%d",&score)!=EOF) { count++; sum+=score; if(score>=90) excelent++; else if(score<60) fail++; } fclose(fp); ave=sum/count; printf("总人数为:%d\n",count); printf("平均成绩为:%.2f\n",ave); printf("优秀人数:%d\n",excelent); printf("不及格人数:%d\n",fail); return 0; }运行结果:
#include <stdio.h> #include <stdlib.h> int main() { int score; //读入的成绩 int excelent=0, fail=0,count=0;//分别代表优秀、不及格人数、总人数 double sum=0,ave; //sum: 成绩和,ave: 平均分 //以输入的方式(ios::in)打开文件 FILE *fp,*sp; fp=fopen("english.dat","r"); if(fp==NULL) { printf("open error!\n"); exit(1); } sp=fopen("statictic.dat","w"); if(sp==NULL) { printf("error\n"); exit(1); } while(fscanf(fp,"%d",&score)!=EOF) //当读取成功…… { count++; sum+=score; if(score>=90) excelent++; else if(score<60) fail++; } ave=sum/count; fprintf(sp,"总人数为:%d\n", count); fprintf(sp,"平均成绩为:%.2f\n", ave); fprintf(sp,"优秀人数:%d\n", excelent); fprintf(sp,"不及格人数:%d\n", fail); fclose(fp); fclose(sp); return 0; }运行结果:
#include<stdio.h> #include<stdlib.h> int main() { int score,r; int best=0,good=0,mid=0,passing=0,fail=0,count=0; double sum=0,ave; FILE *fp; fp=fopen("english.dat","r"); if(fp==NULL) { printf("error"); exit(1); } while(fscanf(fp,"%d",&score)!=EOF) { count++; sum=sum+score; r=score/10; switch(r) { case 10: case 9: best++; break; case 8: good++; break; case 7: mid++; break; case 6: passing++; break; default: fail++; break; } } fclose(fp); ave=sum/count; printf("平均成绩 %.4f\n",ave); int l,i; printf("优秀 "); l=best/100; for(i=0;i<=l;i++) printf("X"); printf(" %d",best); printf("\n良好 "); l=good/100; for(i=0;i<=l;i++) printf("X"); printf(" %d",good); printf("\n中等 "); l=mid/100; for(i=0;i<=l;i++) printf("X"); printf(" %d",mid); printf("\n及格 "); l=passing/100; for(i=0;i<=l;i++) printf("X"); printf(" %d",passing); printf("\n挂科 "); l=fail/100; for(i=0;i<=l;i++) printf("X"); printf(" %d",fail); printf("\n"); return 0; }运行结果:
#include<stdio.h> #include<stdlib.h> int main() { int num; double salary,award,sum; FILE *fp1,*fp2; fp1=fopen("salarylist.txt","r"); if(fp1==NULL) { printf("error"); exit(1); } fp2=fopen("salary.txt","w"); if(fp2==NULL) { printf("error2"); exit(1); } while(fscanf(fp1,"%d %lf %lf %lf",&num,&salary,&award,&sum)!=EOF) { award*=1.2; fprintf(fp2,"%d %.2lf %.2lf\n",num,salary,award+salary); } fclose(fp1); fclose(fp2); printf("over!"); return 0; }运行结果: