Thread: counting the number of words

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    counting the number of words

    hi everyone,

    for some reason im getting a segfault when i run this code to count how many words there are in the file...also if theres a better way to do it ..please let me know thanks...

    thanks for any help
    Code:
    int fileLength(FILE *file) {
    
    	int file_len=0;	
        char line[LINESIZE];
    	char *linePtr = line;
    
            while (fgets(line, LINESIZE, file) != NULL) {
    
    			while(*linePtr != "\0") {
    				
    				/* move through non words */
    				while((isspace(*linePtr)) || (ispunct(*linePtr)))
    					linePtr++;
    				
    				/* reached a word */
    				file_len++;
    				linePtr++;
    				
    				/* move off the word */
    				while(isalpha(*linePtr))
    					linePtr++;
    
    			}
    		}
    
    		return file_len;
    }
    Last edited by ccoder01; 05-28-2004 at 06:13 PM.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    no worries...i figured it out... code below

    Code:
    int fileLength(FILE *file) {
    
    	int file_len=0;	
        char line[LINESIZE];
    	char *linePtr = line;
    
            while (fgets(line, LINESIZE, file) != NULL) {
    
    			while(*linePtr != '\0') {
    				
    				/* move through non words */
    				while((isspace(*linePtr)) || (ispunct(*linePtr)))
    					linePtr++;
    				
    				/* reached a word */
    				file_len++;
    				linePtr++;
    
    				if(*linePtr == '\0')
    					break;
    				
    				/* move off the word */
    				while(isalpha(*linePtr))
    					linePtr++;
    
    			}
    		}
    		printf("\nFile Length:%d", file_len);
    		return file_len;
    }

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    hey does anyone know a good way to count the number of words in a file...i posted one earlier but it doesnt work very well...

    thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need
    lineptr = line
    each time you start the loop
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 11
    Last Post: 12-31-2007, 02:32 PM
  3. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  4. help concering counting of words in one sentc..
    By Ray Thompson in forum C Programming
    Replies: 1
    Last Post: 11-11-2002, 07:55 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM