strcmp

N

Neel

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)
 
I

Ian Collins

Neel said:
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?
 
N

Neel

Neel said:
Hi friends,
'm getting weird output with strcmp function.
'm unable to detect the error.
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?

I dont know any way to extract data other than strtok
 
I

Ian Collins

Neel said:
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]
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.
 
J

James Kanze

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.
 
J

Juha Nieminen

Neel said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top