Thread: Parsing Question

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    Parsing Question

    Hey guys I'm working on a function
    Code:
    void *copydirectory(void *arg)
    which is passed two consecutive strings separated by null character, these strings are directory names that I need later in the function to open a directory. I was thinking I could use,
    Code:
     char *strtok(char *str, const char *delim);
    or rather
    Code:
    char *strtok_r(char *str, const char *delim, char **saveptr);
    since my function needs to be thread safe. Can I use a null character as a delimiter? For example,
    Code:
    char **saveptr;
    char *dir1, *dir2;
    char *delim = '\0';
    dir1 = strtok_r(arg, delim, &saveptr);
    dir2 = strtok_r(NULL,delim, &saveptr);
    Additionally, would the names being pointed to be non null terminated after the calls to strtok_r? Any tips on other strategies would help.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    No, this will not work. Since the null character marks the end of a string, strtok() wouldn't know that you're overloading its meaning.

    Since you just have two strings concatenated, it's easy to pull them apart:
    Code:
    char arg[] = "/foo\0/bar";
    char *first, *second;
    first = arg;
    second = arg + strlen(arg) + 1;

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    I see. Thanks for your input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 05:31 PM
  3. question about parsing a string??
    By newbie02 in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2003, 10:28 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM