Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. About Strtok

About Strtok

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelptutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    savitri
    wrote on last edited by
    #1

    Hi everybody.. I am doing project on gsm modem.i am reading msg from sim.that read message i want to store into database.for that i have split that sentence so please tell me how to do that.. i am doing like this.. char str[1000]; strcpy(str,m_sResults); MessageBox(str); char delims[] = ","; char* result; result = strtok( str, delims ); while(result != NULL ) { MessageBox(result); result = strtok( NULL, delims ); } but doing like this is impossible to store the values.so i want to store in different variables.please help me out. in m_sResults the sentence is AT+CMGR=1 +CMGR: "REC READ","919860716641","P apu.soni","08/05/22,19:05:34+34",145,4,0,0,"919890081132",145,106 Call me when you reach to your pg... I on the way to room.. OK please help me. thanks in advance, savitri:confused:

    N B J 3 Replies Last reply
    0
    • S savitri

      Hi everybody.. I am doing project on gsm modem.i am reading msg from sim.that read message i want to store into database.for that i have split that sentence so please tell me how to do that.. i am doing like this.. char str[1000]; strcpy(str,m_sResults); MessageBox(str); char delims[] = ","; char* result; result = strtok( str, delims ); while(result != NULL ) { MessageBox(result); result = strtok( NULL, delims ); } but doing like this is impossible to store the values.so i want to store in different variables.please help me out. in m_sResults the sentence is AT+CMGR=1 +CMGR: "REC READ","919860716641","P apu.soni","08/05/22,19:05:34+34",145,4,0,0,"919890081132",145,106 Call me when you reach to your pg... I on the way to room.. OK please help me. thanks in advance, savitri:confused:

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      Don't use strtok since internally it uses a static buffer which will cause problem with calls from multiple threads or simultaneous calls on different strings. Instead use strtok_s/_tcstok_s, a demo -> http://www.tenouk.com/cpluscodesnippet/viewtopic.php?p=474[^] Use vector<string> to store strings that are tokenized by this function.

      Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

      G 1 Reply Last reply
      0
      • S savitri

        Hi everybody.. I am doing project on gsm modem.i am reading msg from sim.that read message i want to store into database.for that i have split that sentence so please tell me how to do that.. i am doing like this.. char str[1000]; strcpy(str,m_sResults); MessageBox(str); char delims[] = ","; char* result; result = strtok( str, delims ); while(result != NULL ) { MessageBox(result); result = strtok( NULL, delims ); } but doing like this is impossible to store the values.so i want to store in different variables.please help me out. in m_sResults the sentence is AT+CMGR=1 +CMGR: "REC READ","919860716641","P apu.soni","08/05/22,19:05:34+34",145,4,0,0,"919890081132",145,106 Call me when you reach to your pg... I on the way to room.. OK please help me. thanks in advance, savitri:confused:

        B Offline
        B Offline
        bob16972
        wrote on last edited by
        #3

        don't use the token related functions for delimited file streams as they are dumb when faced with somthing like... One,Two,Three,,,Six,Seven,... It treats the three consecutive commas as one which could lead to unexpected results if those delimiters should be treated as "nothing" values for their respective indexes.

        1 Reply Last reply
        0
        • S savitri

          Hi everybody.. I am doing project on gsm modem.i am reading msg from sim.that read message i want to store into database.for that i have split that sentence so please tell me how to do that.. i am doing like this.. char str[1000]; strcpy(str,m_sResults); MessageBox(str); char delims[] = ","; char* result; result = strtok( str, delims ); while(result != NULL ) { MessageBox(result); result = strtok( NULL, delims ); } but doing like this is impossible to store the values.so i want to store in different variables.please help me out. in m_sResults the sentence is AT+CMGR=1 +CMGR: "REC READ","919860716641","P apu.soni","08/05/22,19:05:34+34",145,4,0,0,"919890081132",145,106 Call me when you reach to your pg... I on the way to room.. OK please help me. thanks in advance, savitri:confused:

          J Offline
          J Offline
          Jijo Raj
          wrote on last edited by
          #4

          You can also use stringstream to parse strings. Since I couldn't find a reference, i wrote a quick sample. Check it below.

          #include "string"
          #include "vector"
          #include "sstream"
          ...

          string sResult(_T("Hello,can,you,split,me?")); // Your result string.
          istringstream ResultParser( sResult ); // Input string stream.

          string sToken;
          vector vResults;

          // Tokenize strings.
          while ( getline( ResultParser, sToken, ',' ))
          {
          // print tokens to console.
          vResults.push_back( sToken );
          }

          Regards, Jijo.

          _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

          1 Reply Last reply
          0
          • N Nibu babu thomas

            Don't use strtok since internally it uses a static buffer which will cause problem with calls from multiple threads or simultaneous calls on different strings. Instead use strtok_s/_tcstok_s, a demo -> http://www.tenouk.com/cpluscodesnippet/viewtopic.php?p=474[^] Use vector<string> to store strings that are tokenized by this function.

            Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            G Offline
            G Offline
            Graham Bradshaw
            wrote on last edited by
            #5

            Nibu babu thomas wrote:

            cause problem with calls from multiple threads or simultaneous calls

            Mutliple threads is OK, apparently. See http://msdn.microsoft.com/en-us/library/2c8d19sb(VS.71).aspx[^]

            N 1 Reply Last reply
            0
            • G Graham Bradshaw

              Nibu babu thomas wrote:

              cause problem with calls from multiple threads or simultaneous calls

              Mutliple threads is OK, apparently. See http://msdn.microsoft.com/en-us/library/2c8d19sb(VS.71).aspx[^]

              N Offline
              N Offline
              Nibu babu thomas
              wrote on last edited by
              #6

              Graham Bradshaw wrote:

              Mutliple threads is OK, apparently.

              Thanks Graham!

              Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups