METHOD 1: Not suitable for all cases / Preferable for small programs
Simple Logic and Easy to Understand
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
void main()
{
char str[10000],filename[15];
/* The size of string str is 10000 so the program will produce */
/* error if the sourcefile contains more than 10000 characters. */
/* You can edit the source code to remove such errors. */
int i;
FILE *source,*temp;
clrscr();
printf("**************************\n");
printf("* COMMENTS REMOVER \t *\n");
printf("*\tby Ankit Pokhrel *\n");
printf("**************************\n\n");
printf("Enter a filename with extension: ");
gets(filename);
source = fopen(filename,"r"); /* Open the source file */
if(source == NULL)
{
perror("Error ");
getch();
exit(0);
}
temp = fopen("temp.cpp","w"); /* Make a temporary file */
if(temp == NULL)
{
perror("Error ");
getch();
exit(0);
}
/* Copy all the contents of file to variable string str */
i = 0;
do
{
str[i] = fgetc(source);
} while(str[i++] != EOF);
str[i-1] = '\0'; /* Remove EOF character with end - of - string marker '\0' */
i = 0;
while(str[i] != '\0')
{
if(str[i] == '/' && str [i+1] == '*') /* If comment */
{
i += 2; /* Jump first two characters '/' and '*' */
do
{
i++; /* Continuously move the cursor of the file */
} while(str[i] != '*' || str[i+1] != '/'); /* until the end of comment is reached */
i += 2; /* Jumps the last two characters i.e, '*' and '/' */
}
else
fputc(str[i++],temp); /* Copy to temp */
}
fcloseall(); /* Close both files */
remove(filename); /* Remove original file */
rename("temp.cpp",filename); /* Rename temporary file to original filename */
printf("Comments removed successfully.");
getch();
}
METHOD 2: Suitable for all cases / Preferable for Large Programs
Complex Logic and Difficult to Understand
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
void main()
{
char ch,filename[15];
FILE *source,*temp;
clrscr();
printf("**************************\n");
printf("* COMMENTS REMOVER \t *\n");
printf("*\tby Ankit Pokhrel *\n");
printf("**************************\n\n");
printf("Enter a filename with extension: ");
gets(filename);
source = fopen(filename,"r"); /* Open the source file */
if(source == NULL)
{
perror("Error ");
getch();
exit(0);
}
temp = fopen("temp.cpp","w"); /* Make a temporary file */
if(temp == NULL)
{
perror("Error ");
getch();
exit(0);
}
while(ch != EOF) /* Until end of file is reached */
{
ch = fgetc(source); /* Get a character */
if(ch == '/')
{
ch = fgetc(source); /* Get another character */
if(ch == '*')
{
do
{
ch = fgetc(source); /* Continuously get a character */
if(ch == EOF) /* If end of file is reached inside the loop */
break;
if(ch == '*')
{
ch = fgetc(source);
if(ch == '/') /* unless '/' is found */
break;
}
} while(1); /* Until breaking condition is not reached */
}
else
{
fputc('/',temp);
fputc(ch,temp);
}
}
else
{
if(ch != EOF) /* To exclude last EOF character */
fputc(ch,temp);
}
}
fcloseall(); /* Close both files */
remove(filename); /* Remove original file */
rename("temp.cpp",filename); /* Rename temporary file to original filename */
printf("Comments removed successfully.");
getch();
}
Friday, September 4, 2009
COMMENTS REMOVER
Labels: Nice Programs
Posted by Unknown at 1:18 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment