[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Ashit Vora

10/7/2008 12:18:00 AM

Hi friends,
'm getting weird output with strcmp function.
'm unable to detect the error.

code is:

string myline="[init]";
line=myline.c_str();
line[strlen(line)]='\0';

char *value=strtok(line,"=");

cout<<strlen(value)<<endl;


if(strcmp(value,"[init]")==0){
cout<<"found"<<endl;
}

even though the value assigned to line is "[init]" (and it prints too
when I do cout) but it doesnt display "found" (satisfy if condition)
7 Answers

Ian Collins

10/7/2008 12:29:00 AM

0

Neel wrote:
> Hi friends,
> 'm getting weird output with strcmp function.
> 'm unable to detect the error.
>
> code is:
>
> string myline="[init]";
> line=myline.c_str();

What is line?

> line[strlen(line)]='\0';
>
myline.c_str() returns a const char*, if you are attempting to modify
that data, all bets are off.

> char *value=strtok(line,"=");
>
Same here, strtok expects a modifiable (char*) C -style string for its
first input.

> cout<<strlen(value)<<endl;
>
>
> if(strcmp(value,"[init]")==0){
> cout<<"found"<<endl;
> }
>
> even though the value assigned to line is "[init]" (and it prints too
> when I do cout) but it doesnt display "found" (satisfy if condition)

Why mess about with C's archaic string manipulations when your starting
data is a std::string?

--
Ian Collins.

Ashit Vora

10/7/2008 12:49:00 AM

0

On Oct 6, 5:29 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Neel wrote:
> > Hi friends,
> > 'm getting weird output with strcmp function.
> > 'm unable to detect the error.
>
> > code is:
>
> > string myline="[init]";
> > line=myline.c_str();
>
> What is line?
>
> > line[strlen(line)]='\0';
>
> myline.c_str() returns a const char*, if you are attempting to modify
> that data, all bets are off.
>
> >       char *value=strtok(line,"=");
>
> Same here, strtok expects a modifiable (char*) C -style string for its
> first input.
>
> >    cout<<strlen(value)<<endl;
>
> >    if(strcmp(value,"[init]")==0){
> >            cout<<"found"<<endl;
> >    }
>
> > even though the value assigned to line is "[init]" (and it prints too
> > when I do cout) but it doesnt display "found" (satisfy if condition)
>
> Why mess about with C's archaic string manipulations when your starting
> data is a std::string?
>
> --
> Ian Collins.

I dont know any way to extract data other than strtok

Ian Collins

10/7/2008 2:03:00 AM

0

Neel wrote:
> On Oct 6, 5:29 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> Neel wrote:

>>> even though the value assigned to line is "[init]" (and it prints too
>>> when I do cout) but it doesnt display "found" (satisfy if condition)
>> Why mess about with C's archaic string manipulations when your starting
>> data is a std::string?

[please trim responses and don't quote signatures]

>> Ian Collins.
>
> I dont know any way to extract data other than strtok

Invest some time in learning how how to use std::string. it's well worth
the effort.

--
Ian Collins.

James Kanze

10/7/2008 11:10:00 AM

0

On Oct 7, 2:29 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Neel wrote:

> > 'm getting weird output with strcmp function.
> > 'm unable to detect the error.

> > code is:

> > string myline="[init]";
> > line=myline.c_str();

> What is line?

> > line[strlen(line)]='\0';

> myline.c_str() returns a const char*, if you are attempting to
> modify that data, all bets are off.

And even if it was a copy: if line is correctly '\0' terminated,
this line isn't necessar, and if it isn't, strlen doesn't work.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jeff Schwab

10/7/2008 3:58:00 PM

0

Ian Collins wrote:
> Neel wrote:
>> On Oct 6, 5:29 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>>> Neel wrote:
>
>>>> even though the value assigned to line is "[init]" (and it prints too
>>>> when I do cout) but it doesnt display "found" (satisfy if condition)
>>> Why mess about with C's archaic string manipulations when your starting
>>> data is a std::string?
>
> [please trim responses and don't quote signatures]
>
>>> Ian Collins.
>> I dont know any way to extract data other than strtok
>
> Invest some time in learning how how to use std::string. it's well worth
> the effort.

See also std::istream_iterator and std::stringstream.

Juha Nieminen

10/7/2008 7:17:00 PM

0

Neel wrote:
> line[strlen(line)]='\0';

Exactly how do you expect strlen to be able to calculate the length of
the string if it isn't null-terminated already?

ytrembla

10/8/2008 9:25:00 AM

0

In article <25c2b063-5f01-420b-987b-21958851275e@v39g2000pro.googlegroups.com>,
Neel <a.k.vora@gmail.com> wrote:
>On Oct 6, 5:29 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>>
>> Why mess about with C's archaic string manipulations when your starting
>> data is a std::string?
>>
>
>I dont know any way to extract data other than strtok

On a random linux machine:

$ man strtok
-----------------------------------------------------------------
[... blah blah blah...]
BUGS
Avoid using these functions. If you do use them, note that:

These functions modify their first argument.

These functions cannot be used on constant strings.

The identity of the delimiting character
-------------------------------------------------------------------

That should be enough to motivate you never again to use this function
and to learn better tools.


Yannick