Thread: how to check if text file is blank

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    40

    Question how to check if text file is blank

    Hi,

    I have written a program that counts number of words in a text file as shown below.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
    	char string[1000]; //to hold the text read out of file
    	char *tokenPtr;//pointer is used to set aside a momory address to store the first token
    	int count_word; 
    	FILE *f;
    	
    	f = fopen("output.txt", "r");
    	count_word = 0;
    
    	fgets(string,1000,f);
    	
    		tokenPtr = strtok(string, " !@#$%^&*()_+=-{}[]|\"':;<,>.?/~`");// string, delimiters used to specify the boundary between regions
    
    		while(tokenPtr != NULL) // stop at NULL
    		{
    			tokenPtr = strtok(NULL, " !@#$%^&*()_+=-{}[]|\"':;<,>.?/~`"); 
    			count_word++;	
    		}
    	
    		printf("\n# of words: %d\n", count_word);
    	}
    		
    		fclose(f);
    		return 0;
    }
    How to get program to correctly print out that # of words is 0 when the text file is empty? I was thinking of either checking of first character is EOF (by way of fgetc) or by checking if string[0] is a null, but I somehow can't get it working.... as of now, program always incorrectly tell me text file has 1 word when the text file is empty.

    Thanks for all help in advance!!
    Last edited by x2x3i5x; 12-23-2009 at 03:40 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Inside your while loop, you are incrementing count_word even though tokenPtr may be null.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Why don't you check if the file size is zero? If it is then do not increment your count_word. Using fseek and ftell can help there, creating a function that performed that should do the trick perhaps.

    I believe what Dino, was pointing out - I would place your count_word++; before your tokenPrt assignment in your while loop.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Swap the call the strtok() with the incrementing of count_word inside your while loop. I'd actually probably write it this way:

    Code:
    for( tokenPtr = strtok( string, delimstring ); tokenPtr; tokenPtr = strtok( NULL, delimstring ) )
        ++count_word;
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Quote Originally Posted by brewbuck View Post
    Swap the call the strtok() with the incrementing of count_word inside your while loop. I'd actually probably write it this way:

    Code:
    for( tokenPtr = strtok( string, delimstring ); tokenPtr; tokenPtr = strtok( NULL, delimstring ) )
        ++count_word;
    Why would that help? What is the difference?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by x2x3i5x View Post
    Why would that help? What is the difference?
    Why would swapping the two statements help, or why would writing it as a for-loop help?

    If you mean the swapping, it's just logically correct, while the code you have now isn't.

    As far as the for-loop, it's equivalent to the while, just a little more compact. Just preference.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Quote Originally Posted by brewbuck View Post
    Why would swapping the two statements help, or why would writing it as a for-loop help?

    If you mean the swapping, it's just logically correct, while the code you have now isn't.

    As far as the for-loop, it's equivalent to the while, just a little more compact. Just preference.

    Ok thank you.
    Last edited by x2x3i5x; 12-24-2009 at 12:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM