Dear Eddy
Thank you for this suggestion. The possibility to use
different tokens opens quite some new possibilities for me.
Best regards
Christian
Edmond Offermann wrote:
> Hi Christian,
>
> I did not follow the whole discussion but if you
> want to split a character string into pieces using
> a bunch of delimiters , TString::Tokenize is the
> way to go:
>
> Suppose, you have the following code with the input:
> "option = "myopt1:myopt2"
>
> MyFunction(const char *option)
> {
> const TString opt = option;
> TObjArray *strL = opt.Tokenize(":");
> const TString opt1 = ((TObjString
> *)strL->At(0))->GetString();
> const TString opt2 = ((TObjString
> *)strL->At(1))->GetString());
>
> delete strL;
> }
>
> The delimiter list can be a list of characters:
>
> TString bla = "aap+noot-mies";
> TObjArray *strL = bla.Tokenize("+-");
>
> Now the TObjeString list strL will contain
> "aap","noot" and "mies" in the positions 0,1 and 2
> resp.
>
> Eddy
>
>
> --- Valeri Fine <fine@bnl.gov> wrote:
>
>>Hello Christian,
>>
>> You STILL have a problem
>> The pointer your new operator returns
>>
>> char *opt = new char[csize];
>>
>> and the pointer you want to use to delete that
>>array MUST match
>>
>> However your statement
>>
>>
>>> opt = strtok(NULL, ":");
>>
>>does change the pointer "opt" and by this reason you
>>can not make free
>>the memory allocated with the "new"
>>
>>
>>You can have avoid that if you use your "opt",
>>"opt1", opt2" solely to
>>create and delete. Use other variables for all
>>internal operations and
>>you will be safe (may be :-)
>>
>>Of course you may research some other ways like the
>>STL "string" class
>>or Qt QString::section(. . .) to do what you want
>>http://doc.trolltech.com/3.3/qstring.html#section )
>>
>> Best regards, Valeri
>>
>>
>>>Dear all
>>>
>>>Thank you all for giving me some more information.
>>>
>>>Sorrowly, Victor´s statement is really bad news.
>>>Is the following code allowed in other compilers:
>>> const Int_t kBufSize = 512; //in global space
>>> char option[kBufSize]; // in different
>>
>>functions
>>
>>>To reply to Valeri:
>>>Suppose, I have the following code with the input:
>>> "options = "myopt1:myopt2"
>>>
>>> MyFunction(const char *option)
>>> {
>>> Int_t csize = strlen(options) + 1;
>>> char *opt = new char[csize];
>>> char *opt1 = new char[csize];
>>> char *opt2 = new char[csize];
>>>
>>> opt = strtok(strcpy(opt, options),":");
>>> opt1 = strcpy(opt1, opt);
>>>
>>> opt = strtok(NULL, ":");
>>> opt2 = strcpy(opt2, opt);
>>>
>>> myClass->MyMethod(opt1, opt2)
>>> {
>>> fString1 = opt1; //TString fString1;
>>> fString2 = opt2; //TString fString2;
>>> }
>>>
>>> delete [] opt2;
>>> delete [] opt1;
>>> delete [] opt;
>>> }
>>>Is this code safe as long as I use TString and not
>>
>>char?
>>
>>>BTW, this is the reason, why I always hated C
>>
>>which I
>>
>>>have never used, and why I try to use the TString
>>
>>class
>>
>>>whenever possible, :-)
>>>
>>>Best regards
>>>Christian
This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:08 MET