C Programming
Computer Programming

C file handling program to Count No of Lines, Blank Lines, Comments in a given Program



Write C file handling program to Count No of Lines, Blank Lines, Comments in a given Program

\* C file handling program to Count No of Lines, Blank Lines, Comments in a given Program *\

# include < stdio.h >
# include < string.h >
# include < stdlib.h >
int   main( )
{

int line_count = 0, n_o_c_l = 0, n_o_n_b_l = 0, n_o_b_l = 0, n_e_c = 0 ;
FILE *fp1 ;
char ch, filename[15] ;

printf("\n Enter the filename to be opened : ") ;
scanf("%s", filename) ;
fp1 = fopen(filename, "r");
if (fp1 == NULL)
{
      printf(" Cannot open file ! \n") ;
      exit(0) ;
}

while ((ch = fgetc(fp1))!= EOF)
{
      if (ch == '\n')
      {
            line_count++ ;
      }
      if (ch == '\n')
      {
            if ((ch = fgetc(fp1)) == '\n')
            {
                  fseek(fp1, -1, 1) ;
                  n_o_b_l++ ;
            }
      }
      if (ch == ';' )
      {
            if ((ch = fgetc(fp1)) == '\n')
            {
            fseek(fp1, -1, 1) ;
            n_e_c++ ;
            }
      }
}

fseek(fp1, 0, 0) ;
while ((ch = fgetc(fp1)) != EOF)
      {
            if (ch == '/')
            {
                  if ((ch = fgetc(fp1)) == '/')
                  {
                        n_o_c_l++ ;
                  }
            }
      }
printf("\n Total no of lines: %d ", line_count) ;
printf("\n Total no of comment line: %d", n_o_c_l) ;
printf("\n Total no of blank lines: %d", n_o_b_l) ;
printf("\n Total no of non blank lines: %d", line_count-n_o_b_l) ;
printf("\n Total no of lines end with semicolon: %d", n_e_c) ;
return 0 ;

}

Output of Program :

Output of C file handling program to Count No of Lines, Blank Lines, Comments in a given Program