将一个C语言源程序文件中所有注释去掉后,存入另一个文件。
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp1,*fp2;
char ch;
if((fp1=fopen("zyw.cpp","r"))==NULL){
printf("File1 open error!\n");
exit(0);
}
if((fp2=fopen("fzhang.cpp","w"))==NULL){
printf("File2 open error!\n");
exit(0);
}
while(!feof(fp1)){
ch=fgetc(fp1);
if(ch=='/'){
ch=fgetc(fp1);
if(ch=='/'){
while(ch!='\n')
ch=fgetc(fp1);
fprintf(fp2,"\n");
}
else if(ch=='*'){
while(ch!='/')
ch=fgetc(fp1);
fprintf(fp2,"\n");
}
else{
fputc('/',fp2);
if(ch!=EOF)
fputc(ch,fp2);
}
}
else{
if(ch!=EOF)
fputc(ch,fp2);
}
}
if(fclose(fp1)){
printf("Can not close the file!\n");
exit(0);
}
if(fclose(fp2)){
printf("Can not close the file!\n");
exit(0);
}
return 0;
}