Question:
count total characters in a text file using c programming?
ace
2013-03-21 21:43:20 UTC
I just need clarification on a question..

I'm supposed to write a program to read a text file and display the following:
a) alphabetic letters //finished with
b) digits // finished with
c) other characters that are not alphabetic letters and not digits //finished with
d) total characters read //need help

The total characters part confused me. Does it mean to count all of the alphabetic letters + other characters or does it want me to count alphabetic letters + digits + other characters??

If so, how would I implement the code into this while loop?

while ((curCh = fgetc(sp1)) != EOF)
{
if (isalpha(curCh))
countCh++;
else if (isdigit(curCh))
digits++;
else
countAllOtherCh++;
}

Also, thanks to @unfaithful for helping me earlier.
Three answers:
PHGDAL
2013-03-21 21:52:57 UTC
Yes just add another counter say "int TotalCharactersRead = 0;" and then increment it outside any of the IF conditions. Something like:



while ((curCh = fgetc(sp1)) != EOF)

{

if (isalpha(curCh))

countCh++;

else if (isdigit(curCh))

digits++;

else

countAllOtherCh++;



TotalCharactersRead++; //This is outside the else condition above

}
Erika
2016-08-04 17:26:22 UTC
Ok. So your enormous mistake was once in comparing with '0' on your at the same time loop. (good, that and the fact that it simplest writes one sentence, no longer 4 of them.) You rather wanted to make use of '' not 'zero'. Note the change? Of course, as already acknowledged you should use fputs() or fprintf() to write out each string alternatively than doing it persona with the aid of persona to your possess. The library services are just a little higher optimized and you may as good use them. Here is some code that handles all 4 sentences. #incorporate #comprise int main( int argc, char *argv[] )     int i;     FILE *fp;     char buf[200];         if ( (fp= fopen( "Exercise3.Txt", "w" )) == NULL )             fprintf( stderr, "can not open the pastime file, "Exercise3.Txt", for output.N" );             return zero;                 for ( i= 1; i <= 4; ++i )             printf( "Enter identify: " );             if ( fgets( buf, sizeof( buf ), stdin ) == NULL )                 fprintf( stderr, "Unable to accept a name from 'stdin'.N" );                 wreck;                         fputs( buf, fp );                 fclose( fp );     return zero; notice that fgets() will most likely have the n personality on the end of the entered sentence. So there's no have to add one for every line written. However, it is possible in some unusual occasions for the last line entered to now not include one. You could add code to check that case and add one, if you wanted the .Txt file that's being written to constantly be targeted to have one at the finish of every line written to it. I did none of that with the above code. (A option to manage that might be to write a subroutine to exchange fgets() which makes use of fgets() but also makes definite each and every line ends with n before returning.)
ʄaçade
2013-03-21 21:56:22 UTC
Phgdal is correct. It means ALL the characters regardless of the kind. Total bytes, basically.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...