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:

  1. C code snippet to generate Kaprekar’s magical constant 6174
  2. C# code snippet for demonstration of data casting!
  3. Dos and Don’ts of Signature File Use
  4. A handy C-code snippet for all the programmers…
  5. Best ‘readme’ File Ever!!!

Filed Under: Archive Categories

About the Author

Om is a dynamic entrepreneur, renowned author, and founder of Webfosys Networks Pvt Ltd that provides IT, content creation, SEO & Website Design services to several clients across the globe. He has written over 12,000 articles, and loves to write Tech Reviews and Auto Reviews Reach him 24/7 - om.thoke86@gmail.com

Comments (4)

Trackback URL | Comments RSS Feed

  1. how to use says:

    Hey man i can compile this, but can you possibly tell how to use it?

  2. om says:

    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 :)

  3. programming freak says:

    Hey thanks, good one!

  4. code learner says:

    Hey good! it works dude… thanks

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.