Categorized | Programming

C-code snippet to print a file by removing comments

Posted on 04 September 2008

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);
}

1 Star2 Stars3 Stars4 Stars5 Stars (17 votes, average: 4.76 out of 5)
Loading ... Loading ...

This post was authored by:

om - who has written 173 posts on Tech Pedia.

Om is a dynamic entrepreneur, founder CEO, Webfosys Networks Pvt Ltd that provides IT, content creation, SEO & Website Design services to several clients across the globe. He has written over 10,000 articles, and he's a platinum level ezine author too. For relevant work mail him to: om@webfosys.com

Contact the author

4 Comments For This Post

  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

Site Sponsors

  • gabriela: i know this phone is so cool because i have it
  • John C. Reid: Kaspersky recently found Malware preinstalled on a new, sealed one of these. Link –>...
  • code learner: Hey good! it works dude… thanks
  • rohit: fortiguard has blocked every thing ..sir even keygen site facebook etc… i want to bypass it …...
  • Darshan Pandit: Great work! Even I’ve been looking for some interesting guide for ActionScripting. You must...

Older Posts

Ads

  • Advertise Here

Topics