strcmp

Discussion in 'C++' started by Neel, Oct 7, 2008.

  1. Neel

    Neel Guest

    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)
    Neel, Oct 7, 2008
    #1
    1. Advertising

  2. Neel

    Ian Collins Guest

    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.
    Ian Collins, Oct 7, 2008
    #2
    1. Advertising

  3. Neel

    Neel Guest

    On Oct 6, 5:29 pm, Ian Collins <> 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
    Neel, Oct 7, 2008
    #3
  4. Neel

    Ian Collins Guest

    Neel wrote:
    > On Oct 6, 5:29 pm, Ian Collins <> 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.
    Ian Collins, Oct 7, 2008
    #4
  5. Neel

    James Kanze Guest

    On Oct 7, 2:29 am, Ian Collins <> 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:
    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
    James Kanze, Oct 7, 2008
    #5
  6. 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?
    Juha Nieminen, Oct 7, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shane Peck

    strcmp problem

    Shane Peck, Sep 19, 2003, in forum: C++
    Replies:
    6
    Views:
    2,357
    Kevin Goodsell
    Sep 22, 2003
  2. muser

    strcmp

    muser, Oct 3, 2003, in forum: C++
    Replies:
    6
    Views:
    1,116
    Frank Schmitt
    Oct 9, 2003
  3. Andrej Hocevar

    please help with strcmp()

    Andrej Hocevar, Jul 19, 2003, in forum: C Programming
    Replies:
    3
    Views:
    327
    Gordon Burditt
    Jul 19, 2003
  4. Allan Bruce

    strcmp but with '\n' as the terrminator

    Allan Bruce, Jul 19, 2003, in forum: C Programming
    Replies:
    53
    Views:
    1,390
    Arthur J. O'Dwyer
    Jul 30, 2003
  5. Robert Mens

    segfault when strcmp

    Robert Mens, Oct 22, 2003, in forum: C Programming
    Replies:
    6
    Views:
    577
    Peter Nilsson
    Oct 23, 2003
Loading...

Share This Page