strtok_s security debugging warnings solving

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?

Answers


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.


Need Your Help

Replacing <hr> line with with a custom glyph

css markdown pseudo-element typography

I would like to replace horizontal line rendered by default by the &lt;hr&gt; tag with three asterisks (horizontally centered). If possible, I want to achieve this with pure CSS, specifically:

How to get outdoors temperature on Bash?

bash geolocation temperature

I need to write a script on bash and on python that will receive temperature from outdoors using geolocation methods.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.