#include
main()
{
char ch1,ch2,ch3,ch4;
FILE *fp1,*fp2;
if((fp1=fopen("D://c programs//常见题//input.txt","r"))==NULL)
{
printf("cann't open the input file/n");
exit(1);
}
if((fp2=fopen("D://c programs//常见题//output.txt","w"))==NULL)
{
printf("cann't open the output file/n");
exit(1);
}
ch1=fgetc(fp1);
ch2=fgetc(fp1);
while(!feof(fp1))
{
if('/'==ch1 && '/'==ch2)
//这里注意,判断条件要细分好,在设及到条件分支语句时,
//要注意,每个分支都要单独的测试到,会更容易维护
{
ch3=fgetc(fp1);
while(!feof(fp1))
{
if(10==ch3)
{
fputc(ch3,fp2);
break;
}
else
{
ch3=fgetc(fp1);
}
}
ch1=fgetc(fp1);
ch2=fgetc(fp1);
}
if('/'==ch1 && '*'==ch2)
{
ch3=fgetc(fp1);
ch4=fgetc(fp1);
while(!feof(fp1))
{
if('*'==ch3 && '/'==ch4)
{
ch1=fgetc(fp1);
ch2=fgetc(fp1);
break;
}
else
{
ch3=ch4;
ch4=fgetc(fp1);
}
}
}
else
{
fputc(ch1,fp2);
ch1=ch2;
ch2=fgetc(fp1);
}
}
fputc(ch1,fp2);
fclose(fp1);
fclose(fp2);
return 1;
}
本文提供了一段C语言程序代码,该代码的功能是从输入文件中读取内容,并将其中的单行(//)和多行(/*...*/)注释删除,最终将处理后的文本写入另一个文件。通过逐行读取并判断是否为注释,从而实现注释的过滤。
1249

被折叠的 条评论
为什么被折叠?



