Reputation: 5170
I have a piece of code which has been complied by Dev C++ correctly. When I tried to execute it using VS2012 express I faced this warning ''warning C4996: '': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details''
I tried to disable security warnings, but I found _CRT_SECURE_NO_WARNINGS option is not available in my VS express. so I had to modify my code to use strtok_s instead of strtok to avoid this warning. However, even the code executes to the end without any errors, something went wrong that the results do not appear. obviously no reading files occur. This is the previous code which worked well on Dev C++
const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
// parse the line
token[0] = strtok(buf, DELIMITER); // first token
if (token[0]) // zero if line is blank
{
for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
{
token[n] = strtok(0, DELIMITER); // subsequent tokens
if (!token[n]) break; // no more tokens
and this is what I tried to execute using VS2012 express
const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
char* next_token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
//char* next_token;
// parse the line
token[0] = strtok_s(buf, DELIMITER, &next_token[0]); // first token
if (token[0]) // zero if line is blank
{
for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
{
token[n] = strtok_s(0, DELIMITER, &next_token[n]); // subsequent tokens
if (!token[n]) break; // no more tokens
What is wrong with the second code?
Upvotes: 0
Views: 2498
Reputation: 409442
It's because the next consecutive call to strtok_s
expects the context
pointer (the last argument to strtok_s
) to filled in by the last call.
You use a different (uninitialized) pointer each call, which means that the context is lost, or rather that it uses a seemingly random pointer as the context.
You should use the outcommented variable next_token
instead of the array.
Upvotes: 1