fgets, tokens

This is a discussion on fgets, tokens within the C Programming forums, part of the General Programming Boards category; I am working on a simple function to take input from a file, and display the input on screen using ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    fgets, tokens

    I am working on a simple function to take input from a file, and display the input on screen using fgets. The input file consists of names of birds. Each bird name has two words, such as "Cedar Waxwing". There is only one bird name per line. Apart from displaying the bird list on screen, I also want to break down the bird name into two tokens. So in this case token1 is Cedar, and token2 is Waxwing. My code compiles correctly but when I execute it I get a lot of garbage output, followed by several lines of "Cedar Waxwing", which is the first bird. None of the other bird names appear on screen. I dont know why this is happening. Any help will be appreciated. Thanks.

    Code:
    #define DELIMITERS " "
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
       char *p;
       char tokens[20][50];
       int x=0;
       int i;
       unsigned char buffer[20];
    
       FILE *fp;
    
       fp = fopen("bird_names.txt","r");
      
       i=0;
    
       while(fgets(buffer, 20, fp)!=NULL){
    
       p=strtok(buffer, DELIMITERS);
       
       while (p!=NULL)
       {
    	   strcpy(tokens[i], p);
    	   p=strtok(NULL, DELIMITERS);
    	   printf("%s %s",tokens[0],tokens[1]);
    	   i++;
       }
       
       }
    	   
       return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > printf("%s %s",tokens[0],tokens[1]);
    On the first pass (when i is still 0), tokens[1] just contains uninitialised data.

    Perhaps
    Code:
    while(fgets(buffer, sizeof buffer, fp)!=NULL){
       i=0;
       p=strtok(buffer, DELIMITERS);
       while ( i < 20 && p!=NULL)
       {
    	   strcpy(tokens[i], p);
    	   p=strtok(NULL, DELIMITERS);
    	   i++;
       }
       printf("%s %s",tokens[0],tokens[1]);
    }
    In order to make for easy elimination of the newline, I would do
    Code:
    #define DELIMITERS " \n"
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    Thanks. That almost did it. Only problem is I still see the first bird repeated continuously. I have about 70 different bird names in my file, each on a new line, but I see the name of the first bird repeated 70 times. I think there is still something wrong with my loop. It is not proceeding to the next line for some reason.

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
       printf("%s %s",tokens[0],tokens[1]);
    you are printing element 0 and 1 of you array in a loop, that is bird number one.
    when you finish your main while loop you do:
    Code:
    for( i = 0; i < 20; i+=2 )
      printf("%s %s\n",tokens[i],tokens[i+1]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

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