strcmp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neel

    strcmp

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

    code is:

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

    char *value=strtok(l ine,"=");

    cout<<strlen(va lue)<<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)
  • Ian Collins

    #2
    Re: strcmp

    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_s tr();
    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(l ine,"=");
    >
    Same here, strtok expects a modifiable (char*) C -style string for its
    first input.
    cout<<strlen(va lue)<<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.

    Comment

    • Neel

      #3
      Re: strcmp

      On Oct 6, 5:29 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      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_s tr();
      >
      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(l ine,"=");
      >
      Same here, strtok expects a modifiable (char*) C -style string for its
      first input.
      >
         cout<<strlen(va lue)<<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

      Comment

      • Ian Collins

        #4
        Re: strcmp

        Neel wrote:
        On Oct 6, 5:29 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >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.

        Comment

        • James Kanze

          #5
          Re: strcmp

          On Oct 7, 2:29 am, Ian Collins <ian-n...@hotmail.co mwrote:
          Neel wrote:
          'm getting weird output with strcmp function.
          'm unable to detect the error.
          code is:
          string myline="[init]";
          line=myline.c_s tr();
          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.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Jeff Schwab

            #6
            Re: strcmp

            Ian Collins wrote:
            Neel wrote:
            >On Oct 6, 5:29 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            >>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_it erator and std::stringstre am.

            Comment

            • Juha Nieminen

              #7
              Re: strcmp

              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?

              Comment

              • Yannick Tremblay

                #8
                Re: strcmp

                In article <25c2b063-5f01-420b-987b-21958851275e@v3 9g2000pro.googl egroups.com>,
                Neel <a.k.vora@gmail .comwrote:
                >On Oct 6, 5:29 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                >>
                >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



                Comment

                Working...