Getting numbers out of a string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HugoFromBOSS
    New Member
    • Sep 2007
    • 38

    Getting numbers out of a string.

    Hello,

    I am using the Linux command "du -sb /root/" to get the number of bytes of the folder root. This of course returns something like:

    1775440949 /root/

    the number of bytes changes. How can I get C++ to take the string with "1775440949 /root/" in it a just get the numbers and put them into a separate string.

    Thanks
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I think you should be able to use a stringstream for this.

    Comment

    • HugoFromBOSS
      New Member
      • Sep 2007
      • 38

      #3
      Thankyou :)

      Oh one other question. The string goes onto two lines like:

      "42342423
      <blank>"

      how do I just get the first line?

      Thanks

      Comment

      • hsn
        New Member
        • Sep 2007
        • 237

        #4
        for your first question to get a number out of a string use the following code
        string str="123";
        int size=str.size() ;
        int sum;
        for(int i=0; i<size; i++)
        sum= (sum*10) str[i]-'0';

        by using this code you will be able to transfer the number.

        for your 2nd question
        use the function getline();
        string str="123\n";
        string str2;
        getline(str,str 2);
        by that str2 = "123";

        good luck and i hope i was helpful

        regards
        hsn

        Comment

        • HugoFromBOSS
          New Member
          • Sep 2007
          • 38

          #5
          WOW Thanks!

          Thanks alot for your help guys!

          Comment

          • HugoFromBOSS
            New Member
            • Sep 2007
            • 38

            #6
            woops!

            Had a bit of a problem...

            cannot convert ‘std::string’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’

            Any ideas?

            Comment

            Working...