Thread: strtok problem

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    strtok problem

    Why is str_token2 getting this value when my delimiter for tempString's tokenizer is "

    test test2 test3
    str_token2 = test test2 test3


    Code:
    while(fgets(buffer, 80, stdin)!=NULL) {
        bg_flag = command_type(buffer);
        chomp(buffer);
        char *tempString[80];
        strcpy(tempString, buffer);
        char *str_token;
        char *str_token2;
        int token_counter = 0;
        str_token = strtok(buffer, " ");
        str_token2 = strtok(tempString, "\"");
    
        if(strcmp(str_token, "cd") == 0){
          while(str_token != NULL){
            if(token_counter == 1){
              chdir(str_token);
            }
            str_token = strtok(NULL, " ");
            token_counter++;
          }
        }else{
          while(str_token != NULL && str_token2 != NULL){
            if(token_counter == 0){
              cmdPath = str_token2;
              cmdArray[0] = basename(str_token2);
            }else{
              if(strcmp(str_token, "&") == 1){
                cmdArray[token_counter] = str_token2;
                //printf("cmdArray[token_counter] = %s\n", cmdArray[token_counter]);
              }
            }
            printf("str_token2 = %s\n", str_token2);
            token_counter++;
            str_token = strtok(NULL, " ");
            str_token2 = strtok(NULL, "\"");
          }
        }
        printf("bg_flag = %d\n", bg_flag);
        if(bg_flag) {
          my_system(cmdPath, cmdArray);
        }else{
          rv = system(buffer);
        }
        if(strcmp(buffer, "exit") == 0) {
          exit(3);
        }
      }
    
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Why is it getting what value?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char *tempString[80];
    strcpy(tempString, buffer);
    
    ...
    
    str_token2 = strtok(tempString, "\"");
    That's wrong. tempString is an array of uninitialized pointers.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 07:36 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM