Instead of
Is there any elegant way of doing that?Code:while(vec.size() < 65536 && (ch = stream.get(), !stream.eof())) vec.push_back(ch);
This is a discussion on Reading blocks of binary data into a vector<unsigned char>? within the C++ Programming forums, part of the General Programming Boards category; Instead of Code: while(vec.size() < 65536 && (ch = stream.get(), !stream.eof())) vec.push_back(ch); Is there any elegant way of doing that?...
Instead of
Is there any elegant way of doing that?Code:while(vec.size() < 65536 && (ch = stream.get(), !stream.eof())) vec.push_back(ch);
Instead of reading one character at a time, read BUFF_SIZE characters at a time.
You can also directly query the stream buffer:
In either case you use the value to determine how many characters to read in each loop iteration.Code:streambuf * pbuf = stream.rdbuf(); streamsize sizeOfBuffer= pbuf->in_avail()!=0? pbuf->in_avail(): BUFF_SIZE; /*in_avail gets the expected number of characters available after an underflow, or number of characters in buffer.*/
Last edited by King Mir; 08-14-2011 at 02:33 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It is possible to use stream iterators.
I don't remember the exact syntax, but that might help you on the way:
std::copy(std::istream_iterator(stream), std::istream_iterator(), std::back_inserter(vec));
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^