Home Forums C Programming How to count characters from text file Reply To: How to count characters from text file

#3373
Humayan
Participant

If you just want to count all the characters , you can do:
 

#include
#include
int main(void)
{
 // open a text file for reading
 FILE * in_file;
 in_file = fopen("c:\programs\help\text1.txt","r");
 

 if ( ! in_file  )
 {
  printf ( "file did not openn" );
  return 1;
 }
    int numberOfCharacters = 0;
 char ch = fgetc ( in_file );
 while ( ch != EOF )
 {
        numberOfCharacters ++;
        ch = fgetc ( in_file );
 }
 printf ( "nthe number of characters is " );
 printf ( "%i" , numberOfCharacters );
 fclose ( in_file );
    return 0;
}