Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL string with iostream

Started by Void Jul 6, 2000 at 3:02 AM 0 replies 550+ views
Original Post
Void
Void
Hi, Can anyone please enlighten me how on to load ALL the contents from a (file)stream object directly to a string object. I tried all sorts of way and the only one I found is the stream.read function but it outputs to an array rather than a string object (which defeats the purpose that I''m trying to avoid char arrays) Thanks..
    

istream if;
string  str;
char buf[128];
//..


if.get(buf, sizeof(buf));    // want to place the contents straight to str


    
Erik Post
Erik Post
If your file is a text file, you could try this:
    
ifstream file;
// ...

stringstream stream;
stream << file.rdbuf();
string str = stream.str();

However, if your file is a binary file, this won''t work. The only way (afaik) to get the contents of a binary file into a string is to use a char buffer, and convert that to a string.

Erik

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.