By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,798 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,798 IT Pros & Developers. It's quick & easy.

reading a file into a string

P: 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
Sep 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
David Bellot schrieb:
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
Sep 28 '06 #2

P: n/a
"David Bellot" <da**********@free.frwrote in message
news:45***********************@news.free.fr...
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...)
Sep 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.