strtok_s in C++

Feb 26, 2014 at 2:21pm
I have a problem, so you can explain me what the "context" in this source_code

This is Stebe jobs's code:

#include <iostream>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string CleanLine(const string& n)
{
string cleanline;
char* char_line = (char*)n.c_str(); // Non-const cast required.

char* token = NULL;
char* context = NULL;
char delims[] = " ,\t\n";

// During the first read, establish the char string and get the first token.
token = strtok_s(char_line, delims, &context); //THIS

// While there are any tokens left in "char_line"...
while (token != NULL)
{
// Accumulate the tokens.
cleanline += token;
cleanline += ' ';

// NOTE: NULL, function just re-uses the context after the first read.
token = strtok_s(NULL, delims, &context); and THIS
}

return cleanline;
}


Thank you very much!
Last edited on Feb 26, 2014 at 2:23pm
Feb 26, 2014 at 2:27pm
strtok_s is not a standard C or C++ function, it is something added by Microsoft. Thus, I find it very funny that this "is Steve Job's code".

http://msdn.microsoft.com/en-us/library/ftsafwz3.aspx

According to the above page, the context parameter is used to store information between calls to the function.
Feb 26, 2014 at 2:37pm
No no, This is code of http://www.cplusplus.com/forum/general/13058/#msg62797

you can explain more material than...!
I don't know what is information between calls to the function!
Thanks
Last edited on Feb 26, 2014 at 2:41pm
Feb 26, 2014 at 2:58pm
strtok is non-reentrant.

That means, while it's being used in one context, it cannot be simultaneously used elsewhere. This is because it uses static data to maintain its state.

If only, that static data could be made a parameter, strtok would be reentrant and we could use it anytime.

Well that's what POSIX did with strtok_r
http://pubs.opengroup.org/onlinepubs/007908799/xsh/strtok.html

... and what Microsoft did with strtok_s
http://msdn.microsoft.com/en-us/library/ftsafwz3.aspx

So yes, yes. L B is right.
Last edited on Feb 26, 2014 at 2:58pm
Feb 26, 2014 at 3:11pm
I understand.
Thank you very much KBW!
Topic archived. No new replies allowed.