How do i copy an entire file into a string

O

owolablo

I'm trying to make a subprogram,
Code:

void file2String(istream& in, string& theFile)


that can read an entire file into a string variable, including line
breaks and i have no idea what im doing. I'm using the GNU g++
compiler on Fedora 4 Linux Distribution.
Thanks in advance for the help
 
V

Victor Bazarov

owolablo said:
I'm trying to make a subprogram,
Code:

void file2String(istream& in, string& theFile)


that can read an entire file into a string variable, including line
breaks and i have no idea what im doing.

Use 'read' member of 'istream' and keep appending what you read to
the string, until an error or end-of-file.
I'm using the GNU g++
compiler on Fedora 4 Linux Distribution.

We don't care.

V
 
K

Kai-Uwe Bux

owolablo said:
I'm trying to make a subprogram,
Code:

void file2String(istream& in, string& theFile)


that can read an entire file into a string variable, including line
breaks and i have no idea what im doing.

Try this:

#include <iterator>

void file2string ( std::istream & in, std::string & str ) {
std::string dummy ( std::istreambuf_iterator<char>( in ),
(std::istreambuf_iterator<char>()) );
str.swap( dummy );
}



Best

Kai-Uwe Bux
 
J

Jens Theisen

owolablo said:
void file2String(istream& in, string& theFile)

that can read an entire file into a string variable, including line
breaks and i have no idea what im doing. I'm using the GNU g++
compiler on Fedora 4 Linux Distribution.
Thanks in advance for the help

in << ifstream(theFile).rdbuf();

To read it into a string make `in' be a stringstream.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top