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 (14 votes, average: 5 out of 5)
Loading ... Loading ...

This post was authored by:

om - who has written 106 posts on Tech Pedia.

Om is a freelance writer cum software engineer! Works for a reputed IT company & provides freelancing writing & web-services to several clients in US, Europe, Asia and all over the world! Owns a website,blog &runs a private franchise business whose clients are in fortune 100 listing! For relevant work contact him to om.thoke86@gmail.com

Contact the author

3 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!

Leave a Reply

Site Sponsors

  • programming freak: And by the way thanks for those useful codes, i am surprised a site like this has C codes? and...
  • programming freak: Keep posting some codes man, i would be glad to find some c/c++/java/html/unix codes, sql queries...
  • programming freak: Hey thanks, good one!
  • Nathaniel Baker: its all good information.. but you forgot one very very important thing…. FAT32 can not...
  • om: Yeah sure it is.. :)

Older Posts

Ads

  • Advertise Here

Topics