Thread: strtok difficulties

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    strtok difficulties

    I have a character array that will have a prefix and then a filename that will be between two square brackets. Something like this;

    get[filename]

    I need to isolate the characters between the brackets. Here is my attempt:

    Code:
    char* get_filename(char selection[]) {
        
        char* ptr;
        char* rv;
        char* temp;
        
        ptr = strtok(selection, "[");
        ptr = strtok(NULL, "[");
        strncpy(temp, ptr, sizeof(ptr));
        ptr = strtok(temp, "]");
        
        strncpy(rv, ptr, sizeof(ptr));
        strcat(rv, "\0");
        fprintf(stdout, "%s\n", rv);
        return rv;
    }
    But of course, this isn't working. Can somebody please help me figure out what I am doing wrong? Thanks!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char* get_filename(char selection[]) {
       char* end;
       char* start = strchr(selection,'[');
       if(start == NULL)
          return NULL
       start ++;
       end = strchr(start ,']');
       if(end == NULL)
          return NULL;
    
       *end = 0;
       return start;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    strtok is a little more power than you need for that. You could use strchr to find the [ and ].

    Depending on exactly what you mean by "isolate", you could do this:
    Code:
    char *str = strchr(selection, '[');
    if (!str) {
        /* no opening bracket */
    }
    *str = '\0'; /* zero-terminate the first part of the given string */
    str++;  /* now str points to beginning of your substring */
    
    char *p = strchr(str, ']');
    if (!p) {
        /* no closing bracket */
    }
    *p = '\0';  /* now str is zero-terminated. */
    
    /* So now selection is "get" (in your example)
       and str (give it a better name!) is "filename" */
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Thanks!

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Have you read how the function works? strtok

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char* get_filename(char selection[])
    {
        char* ptr;
    
        /*
            g-e-t-[-f-i-l-e-n-a-m-e-.-x-x-x-]-\0
        */
    
    
        ptr = strtok(selection, "[]");
        /*
            g-e-t-\0-f-i-l-e-n-a-m-e-.-x-x-x-]-\0
            ^     ^
            ptr   null character
        */
    
    
        ptr = strtok(NULL,"[]");
        /*
            g-e-t-\0-f-i-l-e-n-a-m-e-.-x-x-x-\0-\0
                     ^                       ^
                     ptr                     null char
        */
    
    
        return ptr;
    }
    
    int main(void)
    {
        char testString[] = "get[filename.xxx]";
        char *fname = NULL;
    
        fname = get_filename(testString);
    
        printf("%s\n",fname);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding difficulties
    By luke luvevou in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2009, 03:00 AM
  2. Structure difficulties
    By Tyrant in forum C Programming
    Replies: 8
    Last Post: 11-18-2007, 01:50 PM
  3. Help! Pointer difficulties.
    By Tyrant in forum C Programming
    Replies: 16
    Last Post: 11-17-2007, 01:59 PM
  4. Program difficulties
    By Birdhaus in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2005, 01:18 PM
  5. difficulties w/connection
    By dP munky in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-09-2005, 03:48 PM

Tags for this Thread