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

[C++] from stream to vector

Started by Trillian Jul 27, 2007 at 10:17 AM 1 replies 500+ views
Original Post
Trillian
Trillian
Hello! Could anyone refresh my memory about the stl trickery involved in reading from a stream (actually std::ifstream) to a std::vector. I want to read an amount of chars known only at runtime. I tryed : myVector.reserve(charCount); myStream.read(&myVector[0], sizeof(char)*charCount); But then operator [0] throws an out of range exception.
SiCrane
SiCrane
For the full stream:
  myVector.assign( std::istreambuf_iterator<char>(myStream),                   std::istreambuf_iterator<char>());

For just charCount, use myVector.resize() rather than reserve().
Trillian
Trillian
yay, thanks a lot SiCrane!

Topic Locked

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

Sign in to reply to this topic.