C-code snippet to print a file by removing comments
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 < stdio.h >
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);
}
Related posts:
- C code snippet to generate Kaprekar’s magical constant 6174
- C# code snippet for demonstration of data casting!
- Dos and Don’ts of Signature File Use
- A handy C-code snippet for all the programmers…
- Best ‘readme’ File Ever!!!
Filed Under: Archive Categories

Hey man i can compile this, but can you possibly tell how to use it?
Well all you gotta’ do is to create a random file and write some scrap in it, write few commented lines (starting with // or /* but make sure you close with */) and save ; lets say as file_name.txt (notepad file)
then go to command line,
go to path where you have saved your .exe file of the compiled program
type program_name.exe file_name.txt destination.txt(it could be anything & will be dynamically created as you give name and execute)
That’s it :)
Now go to the path where your file_name lies, you should be able to find a new file (destination.txt) and it would have contents of file_name, which were not commented.. Check it out, it works perfectly and i have been using it to take out all the irrelevant comments of my fellow mates :)
Hey thanks, good one!
Hey good! it works dude… thanks