-2

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:

enter image description here

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:

enter image description here

CCBet
  • 416
  • 1
  • 6
  • 19
  • Why are you accessing `token` without checking whether it is null? – Kerrek SB Mar 10 '16 at 11:58
  • 2
    This is c++, not c. So I highly recommend to use std::string instead of character pointers unsess you **really** have to. It will save you **lots** of trouble! – Frank Puffer Mar 10 '16 at 12:00
  • where and how should I do this in my code, Kerrek ? – CCBet Mar 10 '16 at 12:00
  • @Skyp89 when you have assigned it. as you do with `token =` – default Mar 10 '16 at 12:01
  • 1
    Why are you using `strtok_s` when there are so many better, clearer and safer solutions in C++? – Component 10 Mar 10 '16 at 12:06
  • @Skyp89 http://stackoverflow.com/questions/236129/split-a-string-in-c – default Mar 10 '16 at 12:06
  • I would like a edit on my code if it's possible. I just started learning this strings chapter and your comments don't help me very much at this stage. – CCBet Mar 10 '16 at 12:07
  • Component 10, can you be more specific ? ... your comment doesn't help at all – CCBet Mar 10 '16 at 12:08
  • @Skyp89 In your loop, first you get a possibly null token from `strtok_s`, print the possibly null pointer and _then_ check if it's null. – Joachim Isaksson Mar 10 '16 at 12:09
  • 1
    We're trying to tell you that your choice of splitting a string is not the easiest/safest way to go. Focus your learning elsewhere! If someone tells me they are learning how to fit nails in a wall with a screwdriver, I will probably tell them that a hammer would be easier to use. You're comment basically says that "but I want to learn to fit them with my screwdriver, because I just started learning that". If you **insist** on using strtok, please motivate this need in your question. Otherwise we will continue insisting that you use better ways to split a string. – default Mar 10 '16 at 12:47
  • No, you are not trying anything... tell me what was your better/ easier/faster solution to my problem ??? I don't see any ... like the 4th comment also ... "there are so many better, clearer and safer solutions in C++ " .... and ??? this is helping ??? ... give a solution, don't just say that there are because I was sure of this before posting the question, that's why I posted it ... for solutions ... but I don't see any from you. – CCBet Mar 10 '16 at 13:06
  • I added a link: http://stackoverflow.com/questions/236129/split-a-string-in-c – default Mar 10 '16 at 13:57

2 Answers2

1

You use token as input parameter of cout after the return of strtok_s, without testing whether it is null or not.

This is not safe and is probably the reason of your crash.

Try the following instead:

while (token != NULL)
{
    cout << token << '\n';
    token = strtok_s(NULL, " ", &next_token);
}

By doing so, you could remove the first call to cout.

G. Toupin
  • 54
  • 4
  • Probably this was the cause. I changed the code as you said and now the crash does not appear. – CCBet Mar 10 '16 at 13:21
0

Regarding the following two lines in your code:

token = strtok_s(NULL, " ", &next_token);
cout << token << '\n';;

Can you try explaining to your rubber duck what happens here when strtok_s() returns NULL to indicate that there are no more tokens to retrieve from your string?

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148