Thread: whitespaces

  1. #1
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I have a text file with the line:

    12/16/2008**10:21*AM***************969*happyclown.html

    where *=whitespace. There are 19 whitespaces. I have to use * to show you whitespace, because for some reason, this forum won't allow more than a single whitespace between words or colums. And the tiny little forum compose box makes formatting a nightmare.

    So I wrote a program to count the whitespaces.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        FILE *openfile;
        int whitespacecount = 0, tabspacecount = 0;
        int c;
    
        /* Open file for reading */
    
        if( (openfile = fopen("directorylisting.txt", "rt")) == NULL )
        {
            perror("\ndirectorylisting.txt");
            exit(EXIT_FAILURE);
        }
    
        /* Count whitespaces while not EOF, or end of string */
    
        while( (c = getc(openfile) != EOF) && (c = getc(openfile) != '\0'))
        {
            if (c = ' ')
            whitespacecount++;
            if (c = '\t')                    /* try to count columns of whitespace */
            tabspacecount++;
        }
        printf("The number of whitespaces is %d\n", whitespacecount+1);
        printf("The number of tabspaces is %d\n", tabspacecount+1);
    
        return 0;
    }
    But when I run the program, the output is 29, instead of 19 whitespaces. Why?

    Thanks.
    Last edited by happyclown; 01-06-2009 at 10:28 PM. Reason: added +1

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What I find even stranger is that you're calling getc() twice in each loop (so you're skiping every 2nd char) but yet you get more spaces than you expected, when I would expect you to get about half.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Something else you may want to check is this:
    Code:
    if (c = ' ')
    ...
    if (c = '\t')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dealing with whitespaces whilst reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-12-2008, 01:43 PM
  2. Remove whitespaces from char*
    By desmond5 in forum C Programming
    Replies: 17
    Last Post: 03-10-2008, 11:39 AM
  3. sscanf & whitespaces
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 05-10-2006, 09:06 AM
  4. Whitespaces before macro bracket
    By tretton in forum C Programming
    Replies: 5
    Last Post: 02-21-2006, 09:00 AM
  5. Extracting Whitespaces
    By TechWins in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2002, 07:28 PM