Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > strcmp

Reply
Thread Tools

strcmp

 
 
Neel
Guest
Posts: n/a
 
      10-07-2008
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)
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-07-2008
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.
 
Reply With Quote
 
 
 
 
Neel
Guest
Posts: n/a
 
      10-07-2008
On Oct 6, 5:29*pm, Ian Collins <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-07-2008
Neel wrote:
> On Oct 6, 5:29 pm, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-07-2008
On Oct 7, 2:29 am, Ian Collins <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-07-2008
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segfault when strcmp Robert Mens C Programming 6 10-23-2003 02:53 AM
strcmp muser C++ 6 10-09-2003 08:18 AM
strcmp problem Shane Peck C++ 6 09-22-2003 05:44 PM
strcmp but with '\n' as the terrminator Allan Bruce C Programming 53 07-30-2003 07:38 PM
please help with strcmp() Andrej Hocevar C Programming 3 07-19-2003 04:41 PM



Advertisments