Hi everybody,

I am new in vc and I have many questions to ask before being able to finish
my first C program, thanks to your precious help.
My first problem now is to convert a string to a table of strings;
for example I have as input a string like this:
tcSTRING = "2004/01/01|2004/01/02|2004/01/03"
and I want to convert it to something like:
tcDATE[0]="2004/01/01";
tcDATE[1]="2004/01/02";
tcDATE[2]="2004/01/03";

Thanks for your help :)
(many other newbie questions are coming, so please be patient ;))

Re: string to table by gangadhar

gangadhar
Sun Apr 25 19:09:39 CDT 2004

hi,
If you are sure that the delimiter between the strings is going to be
the pipe (|) then you can use the _tcstok, to split the string into
multiple tokens. Once that is done you can store the tokens into the
array that you want.

#include <tchar.h>

TCHAR tcString[255]; //the stirng is going to be 255 TCHARs
TCHAR tcDate[10][11]; // 10 dates of size 11
TCHAR seps[] = "|";
TCHAR *token;
WORD i=0;
void main( void )
{
token = strtok( string, seps );
while( token != NULL )
{
//copy the token got into the first date array
_tcscpy(tcDate[i++],token);
//get the next token
token = strtok( NULL, seps );
}
}

hth
gangadhar

Majid LAISSI wrote:

> Hi everybody,
>
> I am new in vc and I have many questions to ask before being able to finish
> my first C program, thanks to your precious help.
> My first problem now is to convert a string to a table of strings;
> for example I have as input a string like this:
> tcSTRING = "2004/01/01|2004/01/02|2004/01/03"
> and I want to convert it to something like:
> tcDATE[0]="2004/01/01";
> tcDATE[1]="2004/01/02";
> tcDATE[2]="2004/01/03";
>
> Thanks for your help :)
> (many other newbie questions are coming, so please be patient ;))
>
>
>
>
>
>
>

Re: string to table by Simon

Simon
Mon Apr 26 08:54:33 CDT 2004

"gangadhar npk" <gangadhar_npk_@nospam.plz> wrote in message
news:O$Tpkd4KEHA.620@TK2MSFTNGP10.phx.gbl...
> hi,
> If you are sure that the delimiter between the strings is going to be
> the pipe (|) then you can use the _tcstok, to split the string into
> multiple tokens. Once that is done you can store the tokens into the
> array that you want.

But beware that _tcstok uses a static variable and thus is not re-entrant,
and thus is not threadsafe.

S.



Re: string to table by Majid

Majid
Mon Apr 26 10:43:03 CDT 2004

thank you :)


"Simon Trew" <noneofyour@business.guv> a écrit dans le message news:
OnmzCY5KEHA.1156@TK2MSFTNGP09.phx.gbl...
> "gangadhar npk" <gangadhar_npk_@nospam.plz> wrote in message
> news:O$Tpkd4KEHA.620@TK2MSFTNGP10.phx.gbl...
> > hi,
> > If you are sure that the delimiter between the strings is going to be
> > the pipe (|) then you can use the _tcstok, to split the string into
> > multiple tokens. Once that is done you can store the tokens into the
> > array that you want.
>
> But beware that _tcstok uses a static variable and thus is not re-entrant,
> and thus is not threadsafe.
>
> S.
>
>



Re: string to table by Igor

Igor
Mon Apr 26 15:51:16 CDT 2004

"Simon Trew" <noneofyour@business.guv> wrote in message
news:OnmzCY5KEHA.1156@TK2MSFTNGP09.phx.gbl
> "gangadhar npk" <gangadhar_npk_@nospam.plz> wrote in message
> news:O$Tpkd4KEHA.620@TK2MSFTNGP10.phx.gbl...
>> hi,
>> If you are sure that the delimiter between the strings is going
>> to be the pipe (|) then you can use the _tcstok, to split the string
>> into multiple tokens. Once that is done you can store the tokens
>> into the array that you want.
>
> But beware that _tcstok uses a static variable and thus is not
> re-entrant, and thus is not threadsafe.

It is thread-safe as long as you are using multithreaded CRT, which
keeps the necessary state information in thread-local storage. It's
indeed not reentrant, which means you can get incorrect results with it
even in a single-threaded program. Concurrency and reentrancy are two
entirely different beasts.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Re: string to table by Simon

Simon
Tue Apr 27 05:40:17 CDT 2004


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:OzbL5A9KEHA.2692@tk2msftngp13.phx.gbl...
> It is thread-safe [...] you can get incorrect results with it
> even in a single-threaded program. Concurrency and reentrancy are two
> entirely different beasts.

Yes, I knew that. I was just trying to keep my answer concise without
sacrificing too much accuracy. Perhaps I didn't try hard enough. Instead of
"which means" I should probably have said "which implies".

S.