char *extractSubstring(char *str)
{
    char temp[256];
    char *subString; // the "result"

    printf("%s\n", str); //prints #include "hello.txt"
    strcpy(temp, str); //copies string before tokenizing    

    subString = strtok(str,"\""); // find the first double quote
    subString = strtok(NULL,"\"");   // find the second double quote

    printf("%s\n", subString); //prints hello.txt

    strcpy(str, temp); //<---- the problem

    printf("%s", subString); //prints hello.txt"
    return subString;
}

After I strcpy, why does it add a quotation? When I comment out the 2nd strcpy line, the program works. The printfs will be deleted out of my program. I was just using it to show what was happening with my program.

Can someone please explain to me what is going on? Thank you.

有帮助吗?

解决方案

It is important to realize that strtok() modifies the source string in-place, and returns pointers into it.

Thus, the two calls to strtok() turn str into

#include \0hello.txt\0
           ^ subString points here

(For simplicity, I don't show the final terminating \0).

Now, the second ("problematic") strcpy() changes str back to:

#include "hello.txt"
          ^ subString still points here

This is what makes the " reappear in subString.

One way to fix it is by tokenizing a copy and keeping the original intact. Just make sure that your function doesn't return a pointer to an automatic variable (that would go out of scope the moment the function returns).

其他提示

The first thing to know is that strtok modifies the first argument (str), if this is a constant (such as when calling extractSubstring like so: extractSubstring("#include \"hello.txt\"");) then this leads to undefined behaviour.

You already copy str into temp so you should use temp in your calls to strtok. When the tokenizing is done you should copy subString into a variable that you either allocate on the heap (malloc) or that you pass to extractSubstring as an extra parameter. You can't return a pointer to a local array because the array runs out of scope the the function ends.

So in summary:

subString = strtok(temp, "\"");
subString = strtok(NULL, "\"");

char * ret = malloc(strlen(subString));
strcpy(ret, subString);
ret[strlen(ret)] = '\0';

return ret;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top