Skip navigation.
Home

C-code snippet to print a file by removing comments

programming

If you have a file full of scraps and comments and you wanna print it by removing comments, then here’s a C-code snippet to do the task… #include void main(int argc , char *argv[]) { FILE *fp,*fp1; char ch; clrscr(); fp=fopen(argv[1],”r”); fp1=fopen(argv[2],”w”); while(1) { ch=fgetc(fp); /*fgetc*/ if(ch==EOF) //eof break; else { if(ch==’/') { ch=fgetc(fp); if(ch==’/') { while(1) { ch=fgetc(fp); if(ch==’\n’) goto label; } } if(ch==’*') { while(1) { ch=fgetc(fp); if(ch==’*') { ch=fgetc(fp); if(ch==’/') { while(1) { ch=fgetc(fp); goto label; } } else printf(”*”); } } } else printf(”/”); } } label:fputc(ch,fp1); } fclose(fp); /*closes the file*/ fclose(fp1); }