R
Ravi Uday
Hi,
This code finds the number of non-commented lines in a given source
file.
Works fine except for one condition that i found.
If the Input file is of the below kind :
#include<xyz.h>/* declare headers
declare main*/int main()
{
return 0;
}
/* Input file ends here */
Result:
actual output should be : Number of non-commented lines is 5
Output from code I got is : Number of non-commented lines is 4 !
Can anyone of you point the mistake or better way of going about.
- Ravi
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Maximum chars in any line. If this is crossed then the remaining
bytes are ignored */
#define BYTES 512
/* line is valid if it contains any of the following chars. Otherwise
treated as commented */
#define VALID_CHARS "\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;,'/#{}()*+-0123456789"
int comment_handler ( FILE *fptr, char *c)
{
int ind = 0;
char ch;
if ( ( ch = fgetc ( fptr )) =='*')/* checks for '/*' */
{
while ( !ind )
{
if (fgetc ( fptr ) == '*' )
{
if ( fgetc ( fptr ) == '/')/* checks for end of comment */
{
ind = 1;
*c = fgetc ( fptr);
}
}
}
}
else if ( ch == '/')/* checks cpp comment */
{
while ( (!feof (fptr) ) && ( ch != '\n'))/* end of line check for \n
*/
ch = fgetc ( fptr );
*c = ch;
}
else/* Special case: No comments found */
{
*c = ch;/* Storing next character */
return 0;/* The char is single '/' */
}
return 1;
}
int main (int argc, char *argv[])
{
int i = 0, j = 0, flag = 0;
char ch = 0;
char buffer[BYTES];/* variable holds a max of BYTES (defined) chars in
any line */
FILE *fp = NULL, *fout = NULL;
if ( argc < 3 ) return EXIT_FAILURE;
fp = fopen ( argv[1], "r+b");/* open the source file */
if ( fp == NULL ) return EXIT_FAILURE;
fflush ( stdout);
fout = fopen ( argv[2], "w+b");/* open the output file */
if ( fout == NULL ) return EXIT_FAILURE;
while ( (!feof (fp )) && (!ferror (fp) ))
{
memset ( buffer, 0x00, BYTES);
while ( (j != BYTES-1) && (!flag) )/* Check for max BYTES-1 chars */
{
ch = fgetc ( fp );
if ( ch == '/')
if (comment_handler ( fp, &ch ) == 0)
buffer[j++] = '/';
if ( (ch == '\n') || (feof (fp)))
flag = 1;
/* dont add '\n' or '\r' to the running buffer cause its appended in
the fprintf */
if ( ( ch !='\n' ) && ( ch != '\r'))
buffer[j++] = ch;
}
if (j == BYTES-1)/* line has more than BYTES chars, so ignore them !
*/
{
j++;
while ( (!feof (fp )) && (!ferror (fp) ))
{
ch = fgetc(fp);
if ( ch == '\n')
break;
}
}
if (strpbrk (buffer, VALID_CHARS))
{
fprintf ( fout, "%s\n", buffer);
i++;
}
j = flag = ch = 0;
}
printf ("\n*** Number of non commented lines is : %d ***\n\n", i);
fclose ( fp );
fclose ( fout );
return EXIT_SUCCESS;
}
This code finds the number of non-commented lines in a given source
file.
Works fine except for one condition that i found.
If the Input file is of the below kind :
#include<xyz.h>/* declare headers
declare main*/int main()
{
return 0;
}
/* Input file ends here */
Result:
actual output should be : Number of non-commented lines is 5
Output from code I got is : Number of non-commented lines is 4 !
Can anyone of you point the mistake or better way of going about.
- Ravi
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Maximum chars in any line. If this is crossed then the remaining
bytes are ignored */
#define BYTES 512
/* line is valid if it contains any of the following chars. Otherwise
treated as commented */
#define VALID_CHARS "\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;,'/#{}()*+-0123456789"
int comment_handler ( FILE *fptr, char *c)
{
int ind = 0;
char ch;
if ( ( ch = fgetc ( fptr )) =='*')/* checks for '/*' */
{
while ( !ind )
{
if (fgetc ( fptr ) == '*' )
{
if ( fgetc ( fptr ) == '/')/* checks for end of comment */
{
ind = 1;
*c = fgetc ( fptr);
}
}
}
}
else if ( ch == '/')/* checks cpp comment */
{
while ( (!feof (fptr) ) && ( ch != '\n'))/* end of line check for \n
*/
ch = fgetc ( fptr );
*c = ch;
}
else/* Special case: No comments found */
{
*c = ch;/* Storing next character */
return 0;/* The char is single '/' */
}
return 1;
}
int main (int argc, char *argv[])
{
int i = 0, j = 0, flag = 0;
char ch = 0;
char buffer[BYTES];/* variable holds a max of BYTES (defined) chars in
any line */
FILE *fp = NULL, *fout = NULL;
if ( argc < 3 ) return EXIT_FAILURE;
fp = fopen ( argv[1], "r+b");/* open the source file */
if ( fp == NULL ) return EXIT_FAILURE;
fflush ( stdout);
fout = fopen ( argv[2], "w+b");/* open the output file */
if ( fout == NULL ) return EXIT_FAILURE;
while ( (!feof (fp )) && (!ferror (fp) ))
{
memset ( buffer, 0x00, BYTES);
while ( (j != BYTES-1) && (!flag) )/* Check for max BYTES-1 chars */
{
ch = fgetc ( fp );
if ( ch == '/')
if (comment_handler ( fp, &ch ) == 0)
buffer[j++] = '/';
if ( (ch == '\n') || (feof (fp)))
flag = 1;
/* dont add '\n' or '\r' to the running buffer cause its appended in
the fprintf */
if ( ( ch !='\n' ) && ( ch != '\r'))
buffer[j++] = ch;
}
if (j == BYTES-1)/* line has more than BYTES chars, so ignore them !
*/
{
j++;
while ( (!feof (fp )) && (!ferror (fp) ))
{
ch = fgetc(fp);
if ( ch == '\n')
break;
}
}
if (strpbrk (buffer, VALID_CHARS))
{
fprintf ( fout, "%s\n", buffer);
i++;
}
j = flag = ch = 0;
}
printf ("\n*** Number of non commented lines is : %d ***\n\n", i);
fclose ( fp );
fclose ( fout );
return EXIT_SUCCESS;
}