Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do i copy an entire file into a string

Reply
Thread Tools

How do i copy an entire file into a string

 
 
owolablo
Guest
Posts: n/a
 
      10-11-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-11-2006
owolablo wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      10-11-2006
On Wed, 11 Oct 2006 08:59:48 -0400, "Victor Bazarov"
<(E-Mail Removed)> wrote:
>Use 'read' member of 'istream' and keep appending what you read to
>the string, until an error or end-of-file.


You can also reserve enough space to read the entire file with one
dynamcic allocation (but keep std::string as out param and check the
return values):
http://www.cplusplus.com/ref/iostrea...eam/tellg.html

Best wishes,
Roland Pibinger
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-11-2006
owolablo wrote:

> 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
 
Reply With Quote
 
Jens Theisen
Guest
Posts: n/a
 
      10-11-2006
"owolablo" <(E-Mail Removed)> writes:

> 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.

--
Gruß, Jens
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
xml file formatting from entire file in single string David Southwell Ruby 3 11-04-2009 04:57 AM
Create thumbnails without loading entire file into memory Phil Johnson ASP .Net 3 04-25-2008 07:57 PM
Copying entire file, line by line into another file Jared Wiltshire C++ 8 08-07-2006 09:27 PM



Advertisments