strtok() help...

This is a discussion on strtok() help... within the C Programming forums, part of the General Programming Boards category; This is my first post, so please go easy on me! What I'm trying to do, is to get the ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    4

    Exclamation strtok() help...

    This is my first post, so please go easy on me!

    What I'm trying to do, is to get the names of all the variables in a file (ie a program's code) and put them into an array, which is displayed at the end of the program. But I can't compare what the "token" is with, say "int". I tried it with a text file only containing "int temp = 0;, and it displayed the line ok, and displayed that the 'token' was "int", token[0] was "i", token[1] was "n", etc. But it just wasn't finding it when I was doing if(token == "int").

    Code:
    do {
    	if(feof(p)) break;
    	fgets(line, 60, p); // gets the next line from the file
    	lineCount++;
    	
    	tempDelims = delimiters;
    	token[a] = strtok(line, tempDelims);
    	
    	while(token[a] != NULL){
    		token[a++] = strtok(NULL, tempDelims);
    	}	
    
    } while(1);
    Any ideas?

    Thanks in advance....

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So in short, you're trying to do string comparisons? Consider using strcmp then. Look in your handy C reference book of choice, or look at the man page for it. Also consider looking at this FAQ entry on why feof is a bad choice for controling loops.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    4
    Quote Originally Posted by quzah
    So in short, you're trying to do string comparisons? Consider using strcmp then. Look in your handy C reference book of choice, or look at the man page for it. Also consider looking at this FAQ entry on why feof is a bad choice for controling loops.

    Quzah.
    Thanks

    I tried

    Code:
    if( strcmp(token[a], "int") == 0)
    but it doesn't work? Or am I just being thick

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is token defined as? Is it an array of pointers which you've allocated space for? Or is it just a simple array or pointer? Meaning is it:
    Code:
    char token1[SOMESIZE]; /* like this? ( an array )*/
    char *token2; /* or like this? ( a pointer )*/
    Or is it something:
    Code:
    char *token1[SOMESIZE]; /* like this? ( an array of pointers ) */
    char **token2; /* or this? ( a pointer to a pointer ) */
    If it is one of the first two, then you're doing it wrong, and it should be:
    Code:
    if( strcmp( token, "int" ) == 0 )

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    4
    Ah, right.

    So you can't do the strcmp() function on an element of an array?

    Got it working now ( changed it back to char*token; ), just need to slightly change it!

    Thanks a lot

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So you can't do the strcmp() function on an element of an array?
    If you're working with an array of pointers, then yes, you can reference elements of an array. If you've got a single array, then you don't want to address one single element in the array, because that would be just comparing one letter, not the whole word.
    Code:
    #include <stdio.h>
    int main( void )
    {
        char foo[] = "This is a test.";
        int x;
    
        for( x = 0; foo[x] != '\0'; x++ )
            printf("%c", foo[x] ); /* go through each letter and print it */
    
        printf("%s", foo ); /* print the whole thing at once */
    
        return 0;
    }
    The above illustrates the point I'm making. strcmp is for comparing strings. The whole string, not just portions of it. If you wanted to just compare one letter, then you'd address the particular element of the array in question, just like the loop above does. If you want to compare the whole string, then it's like the second printf statement, where it uses the whole thing at once, and not each individual element.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    4
    Gotcha

    I think I am starting to learn a bit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 10:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 08:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 07:56 PM

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