Click here to Skip to main content
16,015,296 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Split strigs to substrings Pin
Mazdak4-Apr-02 20:08
Mazdak4-Apr-02 20:08 
GeneralRe: Split strigs to substrings Pin
Nektarios Sourligas4-Apr-02 20:24
Nektarios Sourligas4-Apr-02 20:24 
GeneralRe: Split strigs to substrings Pin
Mazdak4-Apr-02 20:45
Mazdak4-Apr-02 20:45 
GeneralRe: Split strigs to substrings Pin
Mazdak4-Apr-02 20:47
Mazdak4-Apr-02 20:47 
GeneralRe: Split strigs to substrings Pin
Joaquín M López Muñoz4-Apr-02 20:24
Joaquín M López Muñoz4-Apr-02 20:24 
GeneralRe: Split strigs to substrings Pin
Paul M Watt4-Apr-02 20:34
mentorPaul M Watt4-Apr-02 20:34 
GeneralRe: Split strigs to substrings Pin
Nektarios Sourligas4-Apr-02 22:45
Nektarios Sourligas4-Apr-02 22:45 
GeneralRe: Split strigs to substrings Pin
Paul M Watt4-Apr-02 23:24
mentorPaul M Watt4-Apr-02 23:24 
Nektarios Sourligas wrote:
vars[index] = strtok(str, " ,");

Earlier I created an array of 10, char* pointers. This is the pragmatic way to hold the variables that you originally mentione din your first question,

var1, var2, var3 ...

Except for in this case, you access the variables like this, using a zero indexed array:

vars[0], vars[1], vars[2] ...

strtok returns the pointer to the next token in your string. Therefore I assign the result of strtok to the current variable. I keep track of the current variable with the index value. index starts at 0. If you wanted to write it out with the 6 or 7 variables that you had it would look like this:

var1 = strtok(str, " ,");
var2 = strtok(NULL, " ,");
var3 = strtok(NULL, " ,");
... and so on.

Nektarios Sourligas wrote:
while (vars[index] && index < 10 /*There is only space for ten strings.*/)

1) vars[index]:

I should have been more explicit with this, another way to write this is (NULL != vars[index]. Basically I am testing the variable for NULL. If strtok reaches the end of the string, and there are no more tokens, then it will return NULL, and we know that we are done parsing the string.

2) index < 10

This is just to be safe, in case you wanted to change the string that I provided. The string that I provided had 7 tokens in it. If you changed the string to something with more than 10 tokens in it, and that test was not there, then most likely an error would occur. I am simply making sure that no more than 10 tokens are assigned from strtok

One more thing that I forgot to mention about strtok, is that it modifies the string that you are parsing, so if you want to keep the original string intact, you will want to make a copy of that string, and use the temporary version of it, like so:

char originalStr = "This is the original string.";
char *tempStr;

// Get the length of the original string.
int iLen = strlen(originalStr);
// Allocate memory for the string, +1 for the terminating NULL.
tempStr = new char[iLen+1];
// Check the return value, as long as the new pointer is not NULL, then the allocation succeeded.
if (NULL == tempStr)
{
  // The allocation failed.
}

// use your temp string.
char* var1 = strtok(tempStr, " ");
...

// delete the memory in the temp string when you are done.
delete[] tempStr;


Good Luck
GeneralRe: Split strigs to substrings Pin
Michael Dunn5-Apr-02 16:42
sitebuilderMichael Dunn5-Apr-02 16:42 
GeneralCOM Pin
4-Apr-02 17:51
suss4-Apr-02 17:51 
GeneralRe: COM Pin
Paul M Watt4-Apr-02 17:56
mentorPaul M Watt4-Apr-02 17:56 
GeneralRe: COM Pin
Prem Kumar4-Apr-02 21:32
Prem Kumar4-Apr-02 21:32 
GeneralRedefining ATLASSERT Pin
nw603124-Apr-02 15:47
nw603124-Apr-02 15:47 
GeneralRe: Redefining ATLASSERT Pin
Christian Graus4-Apr-02 16:14
protectorChristian Graus4-Apr-02 16:14 
GeneralRe: Redefining ATLASSERT Pin
Gerald Schwab4-Apr-02 16:23
Gerald Schwab4-Apr-02 16:23 
GeneralRe: Redefining ATLASSERT Pin
nw603124-Apr-02 17:39
nw603124-Apr-02 17:39 
GeneralRe: Redefining ATLASSERT Pin
Gerald Schwab4-Apr-02 19:04
Gerald Schwab4-Apr-02 19:04 
GeneralRe: Redefining ATLASSERT Pin
Le centriste5-Apr-02 7:35
Le centriste5-Apr-02 7:35 
QuestionHow do I print a dialog box? Pin
atomicluis4-Apr-02 15:44
atomicluis4-Apr-02 15:44 
AnswerRe: How do I print a dialog box? Pin
Christian Graus4-Apr-02 16:03
protectorChristian Graus4-Apr-02 16:03 
GeneralRe: How do I print a dialog box? Pin
Jon Hulatt4-Apr-02 21:40
Jon Hulatt4-Apr-02 21:40 
GeneralRe: How do I print a dialog box? Pin
Christian Graus4-Apr-02 21:45
protectorChristian Graus4-Apr-02 21:45 
AnswerRe: How do I print a dialog box? Pin
Mazdak4-Apr-02 19:36
Mazdak4-Apr-02 19:36 
Generala couple of questions Pin
Dor4-Apr-02 15:00
Dor4-Apr-02 15:00 
GeneralRe: a couple of questions Pin
albean5-Apr-02 4:44
albean5-Apr-02 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.