problem using strlen

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

    problem using strlen

    What is worng with my code.
    I can reads all the lines all right but it doesn't return me the length
    of lines.
    It returns 0 for each line

    #include <fstream>
    #include <istream>
    #include <iostream>
    #include <ostream>
    #include <string>

    int main()
    {
    fstream in("example.txt ");
    static string line;
    int len = line.length();
    while(getline(i n,line))
    {
    cout<< line <<'\n';
    cout<<len<<endl ;
    }
    return 0;
    }

  • Stefan Näwe

    #2
    Re: problem using strlen

    ricky schrieb:
    What is worng with my code.
    I can reads all the lines all right but it doesn't return me the length
    of lines.
    It returns 0 for each line
    >
    #include <fstream>
    #include <istream>
    #include <iostream>
    #include <ostream>
    #include <string>
    >
    int main()
    {
    fstream in("example.txt ");
    static string line;
    int len = line.length();
    How often does this ^^^^^ get executed ?
    while(getline(i n,line))
    {
    cout<< line <<'\n';
    cout<<len<<endl ;
    }
    return 0;
    }
    >
    /S
    --
    Stefan Naewe
    naewe.s_AT_atla s_DOT_de

    Comment

    • Siam

      #3
      Re: problem using strlen

      You're creating an empty string (line)....and setting its length to the
      len variable. As you then put strings into your line, the len variable
      is never recalculated... it remains at what it started at i.e. 0. In
      other words, the len variable doesn't dynamically update itself every
      time you're line changes...you need to explicitly recalculate the
      string length each time...ie.

      while(getline(i n,line))
      {
      len = line.length();
      cout<< line <<'\n';
      cout<<len<<endl ;
      }

      ricky wrote:
      What is worng with my code.
      I can reads all the lines all right but it doesn't return me the length
      of lines.
      It returns 0 for each line
      >
      #include <fstream>
      #include <istream>
      #include <iostream>
      #include <ostream>
      #include <string>
      >
      int main()
      {
      fstream in("example.txt ");
      static string line;
      int len = line.length();
      while(getline(i n,line))
      {
      cout<< line <<'\n';
      cout<<len<<endl ;
      }
      return 0;
      }

      Comment

      • Eric Pruneau

        #4
        Re: problem using strlen


        "ricky" <ricky_nme@yaho o.co.ina écrit dans le message de news:
        1154438359.9764 88.176400@i42g2 00...legr oups.com...
        What is worng with my code.
        I can reads all the lines all right but it doesn't return me the length
        of lines.
        It returns 0 for each line
        >
        #include <fstream>
        #include <istream>
        #include <iostream>
        #include <ostream>
        #include <string>
        >
        int main()
        {
        fstream in("example.txt ");
        static string line;
        int len = line.length();
        this should gives you 0
        while(getline(i n,line))
        extract a line from the text file.. ok
        {
        cout<< line <<'\n';
        print the line
        cout<<len<<endl ;
        print len which is still 0 because you get the lenght before getting the
        string
        }
        return 0;
        oupss forget to close the file....
        }
        >

        Comment

        • ricky

          #5
          Re: problem using strlen

          Great!! that worked
          thanks Siam...
          Siam wrote:
          You're creating an empty string (line)....and setting its length to the
          len variable. As you then put strings into your line, the len variable
          is never recalculated... it remains at what it started at i.e. 0. In
          other words, the len variable doesn't dynamically update itself every
          time you're line changes...you need to explicitly recalculate the
          string length each time...ie.
          >
          while(getline(i n,line))
          {
          len = line.length();
          cout<< line <<'\n';
          cout<<len<<endl ;
          }
          >
          ricky wrote:
          What is worng with my code.
          I can reads all the lines all right but it doesn't return me the length
          of lines.
          It returns 0 for each line

          #include <fstream>
          #include <istream>
          #include <iostream>
          #include <ostream>
          #include <string>

          int main()
          {
          fstream in("example.txt ");
          static string line;
          int len = line.length();
          while(getline(i n,line))
          {
          cout<< line <<'\n';
          cout<<len<<endl ;
          }
          return 0;
          }

          Comment

          • ricky

            #6
            Re: problem using strlen

            Yeah it is working now.
            thanks to all of u.
            Eric Pruneau wrote:
            "ricky" <ricky_nme@yaho o.co.ina écrit dans le message de news:
            1154438359.9764 88.176400@i42g2 00...legr oups.com...
            What is worng with my code.
            I can reads all the lines all right but it doesn't return me the length
            of lines.
            It returns 0 for each line

            #include <fstream>
            #include <istream>
            #include <iostream>
            #include <ostream>
            #include <string>

            int main()
            {
            fstream in("example.txt ");
            static string line;
            int len = line.length();
            >
            this should gives you 0
            >
            while(getline(i n,line))
            >
            extract a line from the text file.. ok
            {
            cout<< line <<'\n';
            print the line
            cout<<len<<endl ;
            print len which is still 0 because you get the lenght before getting the
            string
            }
            return 0;
            oupss forget to close the file....
            }

            Comment

            • Default User

              #7
              Re: problem using strlen

              Siam wrote:
              You're creating an empty string (line)
              Please see my .sig below.



              Brian
              --
              Please don't top-post. Your replies belong following or interspersed
              with properly trimmed quotes. See the majority of other posts in the
              newsgroup, or the group FAQ list:
              <http://www.parashift.c om/c++-faq-lite/how-to-post.htm>

              Comment

              • Default User

                #8
                Re: problem using strlen

                ricky wrote:
                Great!! that worked
                Please see my .sig below.



                Brian
                --
                Please don't top-post. Your replies belong following or interspersed
                with properly trimmed quotes. See the majority of other posts in the
                newsgroup, or the group FAQ list:
                <http://www.parashift.c om/c++-faq-lite/how-to-post.htm>

                Comment

                • Marcus Kwok

                  #9
                  Re: problem using strlen

                  Eric Pruneau <epruneau@infin ition.comwrote:
                  >
                  "ricky" <ricky_nme@yaho o.co.ina ?crit dans le message de news:
                  1154438359.9764 88.176400@i42g2 00...legr oups.com...
                  >What is worng with my code.
                  >I can reads all the lines all right but it doesn't return me the length
                  >of lines.
                  >It returns 0 for each line
                  >>
                  >#include <fstream>
                  >#include <istream>
                  >#include <iostream>
                  >#include <ostream>
                  >#include <string>
                  >>
                  >int main()
                  >{
                  >fstream in("example.txt ");
                  [code elided]
                  oupss forget to close the file....
                  >
                  >}
                  It is not necessary, since fstream's destructor will take care of that
                  when "in" goes out of scope. That is the point of RAII.

                  --
                  Marcus Kwok
                  Replace 'invalid' with 'net' to reply

                  Comment

                  Working...