I am trying to achieve something like this:
while (ifstream has not been entirely read)
{
read a chunk of data into a buffer that has size BUFLEN
write this buffer to ostream
}
At first I tried to achieve this by using ifstream.eof() as my while condition, but I’ve heard that this is not the way to go. I’ve been looking at std::ios::ifstream’s other functions, but can’t figure out what else to use.
PS: I am using a buffer because the file that is being transferred can get very big.
The iostream classes take care of all necessary buffering, so you don’t
have to. The usual idiom to copy an entire file is just:
iostream takes care of all of the necessary buffering. (This is a
somewhat unusual use of
<<, since it doesn’t format. Historicalreasons, no doubt.)
If you need the loop, perhaps because you want to do some
transformations on the data before rewriting it, then it’s a little
tricker, since
istream::read“fails” unless it reads therequested number of characters. Because of this, you have to also check
how many characters were read, and process them even if the read failed:
This is fairly ugly; a better solution might be to wrap the buffer in a
class, and define an
operator<<for this class.