About Strtok
-
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:
-
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:
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
-
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:
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.
-
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:
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.
-
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
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[^]
-
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[^]
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