DIGIT-ARC read-only archive · snapshot: July 23, 2026

count number of whitespaces in C

3 posts · originally from https://geek.digit.in/community/threads/count-number-of-whitespaces-in-c.140499/

A
abhijangda Original post #1
May 10, 2011 at 1:10 AM
Hello everybody, I was doing a simple program to count number of whitespaces in a text file but I am stuck at it.
Here is the code

Code:
void main()
{
    FILE *from;
    int ch,count;
    int c;
    from = fopen("newfile","r");
    
    while ((c = fgetc(from)) != EOF)
    { 
        if (c == '\n' || c == '\t' || c == ' ')
        {
            count++;
           
            }
        }
    printf("%d",count);
    fclose(from);
    }

and in the file it is written

HELLO EVERY ONE \n

and it prints count in millions!!
whats the problem here please tell me!!
A
May 10, 2011 at 9:36 AM
ooppsss!!! Didn't remember to that
thx!!!