0

I am trying to split my actual key on dot and then extract all the fields after splitting it on dot.

My key would look like something this -

t26.example.1136580077.colox

Below is the code I have which I was in the impression, it should work fine but after that I figured it out that it is for windows only and I am running my code on ubuntu and because of that reason I always get -

 error: âstrtok_sâ was not declared in this scope

Below is my code

if(key) {
    vector<string> res;
    char* p;
    char* totken = strtok_s(key, ".", &p);
    while(totken != NULL)
    {
        res.push_back(totken);
        totken = strtok_s(NULL, ".", &p);
    }

    string field1 = res[0]; // this should be t26
    string field2 = res[1]; // this should be example
    uint64_t field3 = atoi(res[2].c_str()); // this should be 1136580077
    string field4 = res[3]; // this should be colox

    cout<<field1<<" "<<field2<<" "<<field3<<" "<<field4<<endl;
}         

I am running Ubuntu 12.04 and g++ version is -

g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3

Is there any way to do the same thing using plain strtok as I don't want to use istringstream as strtok will be more efficient as compared to istringstream..

AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • `strtok_r` is the Posix equivalent of Windows' `strtok_s`. To use plain `strtok`, just get rid of `p` and the third argument to each call, but beware that it's not thread-safe. – Mike Seymour Nov 26 '13 at 18:25
  • ...or enjoy the C idiom and `#ifdef WINDOWS strtok_s #else strtok_r` – jiveturkey Nov 26 '13 at 20:19
  • @jnbbender: Can you please provide an example on how to do that? Thanks for the help.. – AKIWEB Nov 27 '13 at 19:17
  • @TrekkieTechieT-T what exactly is unclear to you? `was not declared in this scope` means exactly what is says :) – emesx Nov 27 '13 at 20:27
  • I am running it on ubuntu machine and strtok_s is for windows only so not sure how to fix this problemm.. – AKIWEB Nov 27 '13 at 20:31
  • Sorry to get back so late. `#ifdef WINDOWS #define TOKEN(x, y, z) strtok_s( (x), (y), (z) ) #else #define TOKEN(x, y, z) strtok_r( (x), (y), (z) ) #endif`. You'll have to play with the macros, they're probably too simple but should work for the example you gave. Replace `strtok_s` with `TOKEN` and you should be okay on both systems. – jiveturkey Dec 03 '13 at 14:23

0 Answers0