strtok problem

This is a discussion on strtok problem within the C Programming forums, part of the General Programming Boards category; Why is str_token2 getting this value when my delimiter for tempString's tokenizer is " test test2 test3 str_token2 = test ...

  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
    Katy, Texas
    Posts
    2,309
    Why is it getting what value?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

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