Re: strtok ( ) help



ern wrote:
> I'm using strtok( ) to capture lines of input. After I call
> "splitCommand", I call strtok( ) again to get the next line. Strtok(
> ) returns NULL (but there is more in the file...). That didn't happen
> before 'splitCommands' entered the picture. The problem is in
> splitCommands( ) somehow modifying the pointer, but I HAVE to call
> that function. Is there a way to make a copy of it or something ?
>
> /* HERE IS MY CODE */
>
> char * lineOfScript;
> const char * delim = "\n";
>
> lineOfScript = strstr(scriptFileBuffer,":preprocess:"); //find
> starting place in script
> lineOfScript = strtok(lineOfScript,delim); //skip a line
> lineOfScript = strtok(NULL,delim); //get next line... now I have a
> command
> splitCommand(lineOfScript); //this is probably where my pointer gets
> messed up...
> lineOfScript = strtok(NULL,delim); //get next line, but strtok
> returns NULL
>
> //Split up command into seperate words.
> //Store words in global array
> int splitCommand(char * command){
> const char * delimeters = " ";
> int i = 0;
> g_UserCommands[0] = strtok(command, " ");
> while(g_UserCommands[i] != NULL && i < 5){
> //printf("%s", g_UserCommands[i]);//for debugging...
> i+=1;
> g_UserCommands[i] = strtok(NULL, " ");
> }
> i=0;
> return 1;
> }



strtok(lineOfScript,delim);

After the initial call, strtok() has to 'remember' the data you've asked it
to parse. To do that here, it makes a copy of whatever lineOfScript pointed
to, and stores it in some internal buffer [that it maintains, and you can't
directly access].


strtok(NULL,delim);

When you call it again, passing NULL as the first param, it simply continues
parsing from wherever it previously left off - i.e., it continues to parse
its internal buffer as set by whatever lineOfScript originally pointed to.


strtok("BOO",delim);

Now, if you call it again with a non-NULL initial param, it forgets whatever
data it was previously storing/working on and resets its internal buffer to
whatever data you've just passed in - a copy of "BOO" in this case. So,
whatever you didn't yet parse - that was originally ref'ed by lineOfScript -
is now lost and forgotten.

Bottom line, you can't do what you're trying to do with ...

p = strtok(p1, p2);

while(strtok(NULL, p2))
{
p3 = strtok(p4, ...);

...
}


--
===============================================================
In an attempt to reduce 'unwanted noise' on the 'signal' ...

Disclaimer:

Any comment/code I contribute might =NOT= be 100% portable, nor
semantically correct [read - 'not 100% pedantically correct'].
I don't care too much about that though, and I reckon it's the
same with most 'visitors' here. However, rest assured that any
'essential' (?) corrections WILL almost certainly appear v.soon
[read - 'to add noise as they see fit, a pedant will be along
shortly'].

WARNINGS: Always read the label. No beside-the-point minutiae
filter supplied. Keep away from children. Do not ignite.
===============================================================


.