View Full Version : Character Counting


barghota
El salamo 3alikom,

Here is a program from Rithchie's book for character counting, when i compile and run it the number of characters isn't displayed...


#include <stdio.h>

main()
{
long nc;

nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}


Would someone plz tell me where can i find the output? Thanks

OneOfOne
it works for me.
you need to pipe the text to it :

[12:15:03][~/programming] -> ./charcount
zsdf
dfsdf (press ctrl+d when you're done typing)
11
[12:15:08][~/programming] -> echo hi | ./charcount
3

peace

sattia
It didnt work with you cuz u didnt send it the appropriate EOF code. You were pressing Ctrl-C while u had to press Ctrl-D (=EOF)

Another way is as shown above to pipe the text u want to it likecat /certain_file | /ur_char_count_codeor by/ur_char_count_code < /certain_file

Note:
getchar is not optimized at all when reading from files (when they are redirected or piped) cuz it reads one by one.