I already read most of the answered questions about this function but I would like to now why is my code printing the tokens and yet breaking. I should use this function to extract the tokens from a string that has only the' '
separator and to do some operations on them, but I tried first to just print the tokens, and if this would have worked perfectly, after to modify the tokens. This is not happening ...
I wrote the code guided by the Help Viewer in Visual Studio 2015:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main()
{ char s[50] = "testing the strtok_s function", *token = NULL, *next_token = NULL;
// Establish string and get the first token:
token = strtok_s(s, " ", &next_token);
cout << token << '\n';
// While there are tokens in s:
while (token != NULL)
{ //Get the next token:
if (token != NULL)
{ token = strtok_s(NULL, " ", &next_token);
cout << token << '\n';;
}
}
}
I get this result in the console, which makes me happy:
But, I also get this error, and a window called iosfwd
pops and a arrow points on a line that probably explains the breaking reason:return (*_First == 0 ? 0
This is the error window: