1748185 Members
4327 Online
108759 Solutions
New Discussion юеВ

strtok question again

 
etienne_7
Occasional Advisor

strtok question again

Hello,
I am trying to parse an ASCII file using strtok. The trouble is some string that I am parsing contain one space and I am trying to get them all at once:
From the string:
Hello how are you
I want the following:
Hello
how
are you
In 3 different strings........
So far I am using the following code:
char* pValue;
pValue = strtok (pText, " \t");
i = 0;
do
{
pValue = strtok (NULL, " \t");
i++;
} while( (pValue != NULL) && (i < Column) );

what I get is:
Hello
how
are
you.....
In fact, I would like strtok to differ from one space to two spaces....
getting only 3 strings not 4:

For the string:
"1234 1235 152153 1 111"

I would like to get only 4 strings:
"1234"
"1235"
"152153"
"1 111"

and not
"1"
12 REPLIES 12
Biswajit Tripathy
Honored Contributor

Re: strtok question again

Did you see my reply to your other strtok question?
Did that help?

Looks like you need to modify the loop to the
following:

do
{
if (i < Column-1)
pValue = strtok (NULL, " \t");
else
pValue = strtok (NULL, NULL);
i++;
} while( (pValue != NULL) && (i < Column) );
:-)
etienne_7
Occasional Advisor

Re: strtok question again

Ok, I will try and did saw your reply but couldn't answer using the thread....will try this time.
The issue is that the single space could be anytim in the string, not only at the end.
etienne_7
Occasional Advisor

Re: strtok question again

Run time error while doing that....

// scan to correct column
char* pValue;
pValue = strtok (pText, " \t");
i = 0;
do
{
pValue = strtok (NULL, NULL);
i++;
} while( (pValue != NULL) && (i < Column) );

This doesn't seem to work...any other idea?
Biswajit Tripathy
Honored Contributor

Re: strtok question again

I don't see the if() condition inside the loop in your
code as I suggested.

What is the value of Column?

- Biswajit
:-)
etienne_7
Occasional Advisor

Re: strtok question again

Column is 5, sometimes 6....Doesn't rreally matter, strtok crashes when you put NULL, NULL
Biswajit Tripathy
Honored Contributor

Re: strtok question again

It does not crash on my 11.11 system. Is your string
NULL terminated?

- Biswajit
:-)
etienne_7
Occasional Advisor

Re: strtok question again

It crashes before the end of the string, at the first pass.
Biswajit Tripathy
Honored Contributor

Re: strtok question again

Can you post your full program?

- Biswajit
:-)
etienne_7
Occasional Advisor

Re: strtok question again

include is the function I am using.
tx