#include<stdio.h>#include<cstdlib>#include<cstring>struct Student
{int num;char name[20];char sex;int age;char address[20];}stu[20];intmain(){
FILE *fp,*fq;struct Student temp;int i=0;//数据数量int j=0,k=0;//打开文件 if((fp=fopen("studentA.dat","r+"))==NULL){printf("cannot open the file");exit(0);}if((fq=fopen("studentB.dat","r+"))==NULL){printf("cannot open the file");exit(0);}//读文件 while(fscanf(fp,"%d%s%c%d%s",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].age,stu[i].address)!=EOF){
i++;}while(fscanf(fq,"%d%s%c%d%s",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].age,stu[i].address)!=EOF){
i++;}//按学号排序for(j=0;j<i-1;j++)//排序 {for(k=i-1;k>j;k--){if(stu[k].num>stu[k-1].num){
temp=stu[k];
stu[k]=stu[k-1];
stu[k-1]=temp;}}}fclose(fp);fclose(fq);//打开文件 if((fp=fopen("studentC.dat","w+"))==NULL){printf("cannot open");exit(0);}//写文件 for(j=0;j<i;j++)fprintf(fp,"%d%s%c%d%s",stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].address);fclose(fp);return0;}
将已知文件中的内容转换成字符串,并过滤掉大写字母和数字,完成后写回。
#include<stdio.h>#include<cstring>#include<cstdlib>intmain(){
FILE *fp;char a[500];int i=0;int j;char ch;if((fp=fopen("c.txt","r+"))==NULL)//打开文件{printf("cannot open the file");exit(0);}while((ch=fgetc(fp))!=EOF)//提取文件中所有符合要求的字符{if(((ch>='A')&&(ch<='Z'))||((ch>='0')&&(ch<='9')))continue;elseif((ch==' ')||(ch=='\n'))continue;else{
a[i]=ch;
i++;}}fclose(fp);if((fp=fopen("c.txt","w+"))==NULL){printf("cannot open the file");exit(0);}fprintf(fp,"%s",a);//输出字符串到文件return0;}