strtok - word counting from a file

This is a discussion on strtok - word counting from a file within the C Programming forums, part of the General Programming Boards category; Hey I have a piece of code that counts the number of words input by the user using strtok. However, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    strtok - word counting from a file

    Hey
    I have a piece of code that counts the number of words input by the user using strtok. However, I am struggling with converting this program to do the same thing but instead of the user inputting the text, it will read from a text file, counting the number of words in the text file.

    Ne tips would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_LINE 80
    
    int main(void)
    {
    	char line[MAX_LINE];
    	
    	char delim[] = " ,.!;:\n";    /* sets up word separators*/
    	char *words, *ptr;
    	
    	int count = 0;
    
    	printf("Enter line of text \n");
    	fgets(line,MAX_LINE,stdin);
    
            ptr= line;
    	
    	while ((words = strtok(ptr, delim)) !=NULL)   
    	      {printf("\n%s\n",words);
    	       count++;
    	       ptr = NULL; 
    	      }
    	 printf("there were %d words in the string \n",count); 
    	       
    	return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    To open the file use fopen(), and then use fgets() just like you already have, expect instead of stdin, specify your own file pointer. And use fclose() to close it at the end.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    Thx hammer, sry to bore u with such a trivial question. Got it all sorted now thx 4 the help

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Unhappy

    Sry to bother agn, using strtok, how could I add to this program by finding the longest and shortest words? Any relevant tips would be greatly appreciated

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    Sry heres the code from the word counting I have:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_LINE 80
    
    int main(void)
    {
    	char line[MAX_LINE];
    	
    	char delim[] = " ,.!:;\n";        /* sets up word separators*/
    	char *words, *ptr;
    	FILE *fptr;
    	int count = 0;
    	int ch;
    	char longest_word;
    
    	fptr = fopen ("test.txt", "r");
    	
    	if (fptr==NULL)
    		printf ("\nCannot open the file\n\n");
    	else	
    	{	
    	while ((ch=fgetc(fptr)) !=EOF)
    	{	fgets(line,MAX_LINE,fptr);
    		ptr= line;
    		
    
    
    	while ((words = strtok(ptr, delim)) !=NULL)   
    	      {
    	       count++;
    	       ptr = NULL;  
    	      }
    
    	 }
    	 	printf("\nNumber of words in the file: %d \n\n",count); 
    		fclose(fptr);
    	}
    	       
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21