Strange problem - strtok

This is a discussion on Strange problem - strtok within the C Programming forums, part of the General Programming Boards category; Hi, I'm using the following code: Code: while(!feof(f)) { fgets (all_id, 100 , f); sscanf (all_id,"%*s %s %s",a_id,d_id); str[i] = ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    Strange problem - strtok

    Hi,

    I'm using the following code:

    Code:
    
    while(!feof(f))
    		  {
    			fgets (all_id, 100 , f);
    			sscanf (all_id,"%*s %s %s",a_id,d_id);
    			str[i] = a_id;
                            printf("\n%s",str[i]);
    			strtok(a_id,"G");
    			ida = strtok (NULL, "G");
    			idsa[i]=atoi(ida);
    			
    			i++;
    			
    		  }
    
    printf("\n%s",str[2]);
    Inside the loop its printing the a_ids but outside its printing the last value of strtok(a_id,"G").

    Why is this happening?
    Appreciate your guidance.

    Thanks,
    Angkar

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Why it's bad to use feof() to control a loop
    Should the first argument of strtok be NULL? strtok(s,ch) searches s for tokens split be ch. So you are splitting NULL into tokens?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Yes because in the first call I'm reaching G and in the next its end...
    My string is of the form NAAG100, NBAG123...
    and I want the 100,123 parts...

  4. #4
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Code:
    strtok(str,'G');
    Would search the array str for the character G. Remember that 'G' is a character constant and "G" is a null terminated string. "G" is 'G' '\0'.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    So, whats the solution?

  6. #6
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Use strtok on the string that contains the numbers. Use 'G' instead of "G".
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Didn't get you. Can you please show an example?

  8. #8
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Try just replacing the instances of "G" in your code with 'G'. Single quotes are for single character constants and double quotes are for strings. If you use double quotes it will add a terminating character at the end ( '\0' ).
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 03:07 PM
  2. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  3. Extremely strange "delete" problem
    By dr_jaymahdi in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2007, 10:06 PM
  4. strange problem
    By kumarqt in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2006, 03:02 AM

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