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



































































September 23rd, 2008 at 9:11 am
Hey man i can compile this, but can you possibly tell how to use it?
September 23rd, 2008 at 12:22 pm
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 :)
October 24th, 2008 at 11:34 am
Hey thanks, good one!