Connecting Tech Pros Worldwide Help | Site Map

reading a file into a string

  #1  
Old September 28th, 2006, 11:35 PM
David Bellot
Guest
 
Posts: n/a
Hi everybody,

I'd like to read part of a file directly into the internal buffer of a
string so that not to use the copy constructor to fill in my string.

What I did before was :

char buffer[1024];
ifstream f("my_file");

f.seekg(my_position);
f.read(buffer, my_size);

string s(buffer);

and the last line cost is that it uses a copy constructor to transfer
data from the buffer into the string's buffer. The file I wanna read is
really large (129Gb) and I don't wanna have to copy 129Gb for nothing.

The string is then used with a library (actually, it's the
Boost::tokenizer which requires a string).

Do you guys have a solution ?

Thanks,
David
  #2  
Old September 28th, 2006, 11:55 PM
Thomas J. Gritzan
Guest
 
Posts: n/a

re: reading a file into a string


David Bellot schrieb:
Quote:
I'd like to read part of a file directly into the internal buffer of a
string so that not to use the copy constructor to fill in my string.
>
What I did before was :
>
char buffer[1024];
ifstream f("my_file");
>
f.seekg(my_position);
f.read(buffer, my_size);
>
string s(buffer);
>
and the last line cost is that it uses a copy constructor to transfer
data from the buffer into the string's buffer. The file I wanna read is
really large (129Gb) and I don't wanna have to copy 129Gb for nothing.
>
The string is then used with a library (actually, it's the
Boost::tokenizer which requires a string).
I'm checked the documentation and I am quite sure that you don't need a
std::string for tokenizer. You can use a begin/end iterator (or pointer) pair.

I hope you don't try to hold the complete file in memory :-)

--
Thomas
http://www.netmeister.org/news/learn2quote.html
  #3  
Old September 29th, 2006, 12:15 AM
Jim Langston
Guest
 
Posts: n/a

re: reading a file into a string


"David Bellot" <david.bellot@free.frwrote in message
news:451c50a8$0$28025$626a54ce@news.free.fr...
Quote:
Hi everybody,
>
I'd like to read part of a file directly into the internal buffer of a
string so that not to use the copy constructor to fill in my string.
>
What I did before was :
>
char buffer[1024];
ifstream f("my_file");
>
f.seekg(my_position);
f.read(buffer, my_size);
>
string s(buffer);
>
and the last line cost is that it uses a copy constructor to transfer data
from the buffer into the string's buffer. The file I wanna read is really
large (129Gb) and I don't wanna have to copy 129Gb for nothing.
>
The string is then used with a library (actually, it's the
Boost::tokenizer which requires a string).
>
Do you guys have a solution ?
One option would be to use a std::vector<charinstead of a std::string.
Preallocate the 1024 bytes and write directly into the vectors memory (which
is allowed) using (I believe) .data().

Of course this won't get you std::strings benifits (substr, etc...)


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading file into string jain236 answers 3 August 20th, 2008 09:50 PM
reading file into string array novacreatura@gmail.com answers 2 March 6th, 2006 07:05 PM
reading file into array James answers 5 November 23rd, 2005 04:15 AM
reading contents of text file into string Mike P answers 5 November 15th, 2005 11:06 AM
Reading unformatted text from stdin Lionel B answers 19 July 23rd, 2005 03:38 AM