saving entire input into 1 string?

This is a discussion on saving entire input into 1 string? within the C++ Programming forums, part of the General Programming Boards category; If i wanted to save entire input till EOF into 1 string, any idea how I would do this? (besides ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    saving entire input into 1 string?

    If i wanted to save entire input till EOF into 1 string, any idea how I would do this?

    (besides getline and concating strings every time, thats the only solution I can think of)

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    Given a std::istream named in, one way is:
    Code:
    std::stringstream ss;
    ss << in.rdbuf();
    std::string str = ss.str();
    But I recall from an old thread that a more efficient method works along the lines of what you have described. Perhaps you can search and find out what exactly was the most efficient method we considered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by rodrigorules View Post
    If i wanted to save entire input till EOF into 1 string, any idea how I would do this?

    (besides getline and concating strings every time, thats the only solution I can think of)

    Thanks
    Personally, I would seek to the end of the file (istream::seekg) to obtain the length of the data (istream::tellg), resize the buffer, then read the entire block at once (istream::read).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Try to keep things simple. If getline works fine in your situation, leave the alternate. Making things complex for yourself will reap no benefits. Curiosity however is fine and a good thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 04:17 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21