Home › Forums › C Programming › How to count characters from text file › Reply To: How to count characters from text file
April 3, 2008 at 1:14 pm
#3373
Humayan
Participant
If you just want to count all the characters , you can do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h><br /> #include <string.h><br /> int main(void)<br /> {<br /> // open a text file for reading<br /> FILE * in_file;<br /> in_file = fopen("c:\programs\help\text1.txt","r");<br /> <br /> <br /> if ( ! in_file )<br /> {<br /> printf ( "file did not openn" );<br /> return 1;<br /> }<br /> int numberOfCharacters = 0;<br /> char ch = fgetc ( in_file );<br /> while ( ch != EOF )<br /> {<br /> numberOfCharacters ++;<br /> ch = fgetc ( in_file );<br /> }<br /> printf ( "nthe number of characters is " );<br /> printf ( "%i" , numberOfCharacters );<br /> fclose ( in_file );<br /> return 0;<br /> } |