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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,638
    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:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  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