Thread: whitespaces

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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