Thread: strtok

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    61

    strtok

    hi again, i have the following code and it has some problem. after doing strtok for the 1st time, when i print out the value of line[0], it returns "hello -a" which is correct. then after the next strtok in the for loop, the result by printing line[0] changes to "hello" i did not do anything to change the value.. so how did it got modified? isit because i did temp = strtok(line[i], ""); and line[i] got changed in the process? please help thanks!
    Code:
    char* string;
    string = "hello -a | world -b";
    char** line;
    char** line2;
    char* temp;
    int i, count;
    i = count = 0;
    //do malloc for line and line 2...
    ......
    .....
    
    /*count number of separated commands */
    for(i = 0; i < strlen(string); i++) {
        if(strcmp(string[1], "|") ==0){
            sector ++;
        }
    }
    
    if(sector) {
       temp = strtok(string, "|");
       while(temp) {
          line[i] = temp;
          i++;
          temp = strtok(NULL, "|");
      }
      printf("&#37;s", line[0]"); //returns hello -a
      for(i = 0; i <= count; i++) {
         temp = strtok(line[i], "");
         line2[i] = temp;
      }
      printf("%s", line[0]");  // returns hello
    }
    Last edited by cstudent; 04-23-2008 at 05:19 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Strtok damages the input string - it replaced the "token" with 0, so you are only seeing part of the string.
    If you don't like the behaviour, you can take a look at my strtok, which is not damaging (the input is always kept intact).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    thanks i will try it out. however, im still interested in knowing how to overcome this problem. am i right to say that when i do temp = strtok(line[i], " "); , something is modified to line?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, say you have a string "Hello World". It's an array of chars as thus: 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'.
    When you print it, it detects the end at the '\0'. But when strtok is run with token " ", the char array will then look like:
    'H', 'e', 'l', 'l', 'o', '\0', 'W', 'o', 'r', 'l', 'd', '\0'.
    (Notice how the space was replaced by '\0'.)
    Therefore, functions think the string ends after "Hello" and stops there.
    That's why strtok is damaging.
    So yes, it modifies the input.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    hahahaha i understand now.. kinda crappy :/ now.. i gotta think of another way to overcome this cos i cant use another your awesome strtok function hopefully most of my brain cells will survive.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cstudent View Post
    cos i cant use another your awesome strtok function hopefully most of my brain cells will survive.
    Any specific reason you can't?
    Or do you simply not understand how it works?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    ah i thought of it. maybe if i create a duplicate of line and use strtok on it instead.... maybe it will work. yea theres a reason. i can only have a set of specific files

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it will work. Not the best way, but sure, it will work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    heh thanks.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    oh btw, how do u duplicate a copy of char **line with char** lineDup ??

    do a loop of line and assign values to lineDup??

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Depends on what char** is. But basically it's just a pointer to pointer, which probably means it's a dynamically allocated 2D array.
    Most likely (assuming you have a buffer with enough space), just loop through it using strcpy(dest[x], src[x]).
    Last edited by Elysia; 04-23-2008 at 05:49 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >Most likely (assuming you have a buffer with enough space), just loop through it using strcmp(dest[x][y], src[x][y]).
    What?

    Malloc enough space for each line* in the new line**
    Copy over the data from the old line* to the new line*
    And then return the line**

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by citizen View Post
    >Most likely (assuming you have a buffer with enough space), just loop through it using strcmp(dest[x][y], src[x][y]).
    What?
    Oops, my bad. src[x] and dest[x] it should be along with strcpy.
    Ah, this isn't my "C day" it seems
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    sigh wat a pain the the ass !

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then use my strtok. No such pain in the ass.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM