K
Karl
Hey everyone!
I've got a quick question on whether std::ifstream is buffered or not.
The reason is that I have a homework assignment that requires me to
benchmark copying files using different buffer sizes. I ended up doing
this using std::istream::readsome() and std:stream::write():
// now try writing to the destination file
std::ifstream sourceStream(sourceFilename.c_str(),
std::ios::binary);
std:fstream destinationStream(destinationFilename.c_str(),
std::ios::binary | std::ios::trunc);
// determine the size of the file
sourceStream.seekg(0, std::ios::end);
const std::streamsize totalLength = sourceStream.tellg();
sourceStream.seekg(0, std::ios::beg);
// now writing the actual data
char buffer[numberOfBytes];
std::streamsize length = 0;
while(length != totalLength)
{
int l = sourceStream.readsome(buffer, numberOfBytes);
destinationStream.write(buffer, l);
length += l;
}
// now close the stream
sourceStream.close();
destinationStream.close();
Now, the validity of benchmarking this could possibly be in question
because std::istream::readsome() could be doing some buffering
(readahead?) in the back, so using different buffer sizes would
possibly be meaningless.
I understand std::cout and std::cin is buffered, and it makes sense
that they are. However, I do not see why std::ifstream and
std:fstream would be buffered because the filesystem and even the
harddrive does some buffering. Wouldn't that be meaningless?
Then again, this could depend on the implementation. I'm not familiar
enough with the C++ STL specification.
Can anyone help me out? Any help would be greatly appreciated.
I've got a quick question on whether std::ifstream is buffered or not.
The reason is that I have a homework assignment that requires me to
benchmark copying files using different buffer sizes. I ended up doing
this using std::istream::readsome() and std:stream::write():
// now try writing to the destination file
std::ifstream sourceStream(sourceFilename.c_str(),
std::ios::binary);
std:fstream destinationStream(destinationFilename.c_str(),
std::ios::binary | std::ios::trunc);
// determine the size of the file
sourceStream.seekg(0, std::ios::end);
const std::streamsize totalLength = sourceStream.tellg();
sourceStream.seekg(0, std::ios::beg);
// now writing the actual data
char buffer[numberOfBytes];
std::streamsize length = 0;
while(length != totalLength)
{
int l = sourceStream.readsome(buffer, numberOfBytes);
destinationStream.write(buffer, l);
length += l;
}
// now close the stream
sourceStream.close();
destinationStream.close();
Now, the validity of benchmarking this could possibly be in question
because std::istream::readsome() could be doing some buffering
(readahead?) in the back, so using different buffer sizes would
possibly be meaningless.
I understand std::cout and std::cin is buffered, and it makes sense
that they are. However, I do not see why std::ifstream and
std:fstream would be buffered because the filesystem and even the
harddrive does some buffering. Wouldn't that be meaningless?
Then again, this could depend on the implementation. I'm not familiar
enough with the C++ STL specification.
Can anyone help me out? Any help would be greatly appreciated.