Can anyone here revised my fucntion.
The job of the function is to read from a file as well as detect comment such as "/*statement*/" and "//Statement.
If the function finds a comment it will skip the whole statement inside the comment premises. if not, enqueue into the queue which is in the function parameters.
"int xprs" is a counter how many expression inside the file. The terminator of each expression is semi-colon ';'.
The expression inside the comment is not counted.
if the expression inside the file are all inside the comment it will report an error.
CODE
int read_file(char filename[], QUEUE Q1, int xprs) {
int eflag = 0,ctr=0;
char temp,cont[2];
FILE *fp = fopen(filename,"rb");
if ( !fp ) {
perror("ERROR: ");
return 0;
}
while ( !feof(fp) ) {
temp = fgetc(fp);
if ( !feof(fp) ) {
/*empty file*/
if ( (temp == ';') && (ctr == 0) ) {
eflag = 1;
break;
}
if ( temp == ';' )
xprs = xprs+1;
if ( temp == '/' ) {
cont[0] = temp;
temp = fgetc(fp);
cont[1] = temp;
if ( (cont[0] == '/') && (cont[1] == '/' ) ) {
while ( temp != ';' )
temp = fgetc(fp);
}
else if ( temp == '*' ) {
while ( temp == ';' )
temp = fgetc(fp);
}
}
else enQueue(temp,Q1);
}
ctr = ctr+1;
}
if ( eflag == 1) ) {
printf("\n\nNo expression in the file.");
getch();
}
return 1;
}
Thanks and God Bless