I try to imitate the first stage of pre-processor, which is to remove comments from a *.c file.
The main principle is that a *.c will be read and the program will create another file *.c1 which is an exact copy of *.c but without comments(c/c++ comments).
Example(in Linux): >./myprog name.c
produce: name.c1
I wrote the following code, but when it runs and create the *.c1 file, I can see (in Linux file image) that the beginning of that file is OK but the other is crap for some unknown reason..
Please help me solve this issue ...
thnx all !!
The code :
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
enum status {OUT , IN_STRING , LEFT_SLASH , IN_COMMENT , RIGHT_STAR , IN_CPP_COM};
int main(int argc , char *argv[])
{
FILE *fd , *new_fd; /* fd -> *.c ; new_fd -> *.c1 */
int ch;
int state = OUT;
int new_file_string_len = strlen(argv[1])+2; /*num of chars in name.c +1 for name.c1*/
char *new_file_name;
new_file_name=(char *) malloc ((new_file_string_len)*sizeof(char));
strcpy(new_file_name , argv[1]);
new_file_name[new_file_string_len-1] = '\0';
new_file_name[new_file_string_len-2] = '1';
if( !(fd = fopen (argv[1],"r") ) )
{
fprintf(stderr,"cannot open file !\n");
exit(0);
}
if( !(new_fd = fopen (new_file_name,"w+") ) )
{
fprintf(stderr,"cannot open file !\n");
exit(0);
}
while ( (ch=fgetc(fd)) != (feof(fd)) )
switch (state)
{
case OUT:
if (ch=='/')
state = LEFT_SLASH;
else
{
fputc(ch,new_fd);
if (ch=='\"')
state = IN_STRING;
}
break; /*OUT*/
case LEFT_SLASH:
if(ch=='*')
state = IN_COMMENT;
else if (ch=='/')
state = IN_CPP_COM;
else
{
fputc('/',new_fd);
fputc(ch,new_fd);
state = OUT;
}
break; /*LEFT_SLASH*/
case IN_COMMENT:
if(ch=='*')
state = RIGHT_STAR;
break; /*IN_COMMENT*/
case IN_CPP_COM:
if(ch=='\n')
{
state = OUT;
fputc('\n',new_fd);
}
break; /*IN_CPP_COM*/
case RIGHT_STAR:
if(ch=='/')
state = OUT;
else if (ch!= '*')
state = IN_COMMENT;
break; /*RIGHT_STAR*/
case IN_STRING:
if(ch=='\"')
state = OUT;
fputc(ch,new_fd);
break; /*IN_STRING*/
} /*switch*/
fclose(fd);
fclose(new_fd);
return 0; /*dummy*/
} /*main()*/