Say I do this (a contrived example):
#include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]) { ifstream ifs(argv[1]); char ch; while(ifs.read(&ch, 1)) { cout << ch; } }
I assume(hope) that the iostream library does some internal buffering here and doesn’t turn this into gazillions of one-byte file-read operations at the OS level.
Is there a way of:
a) Finding out the size of ifstream’s internal buffer?
b) Changing the size of ifstream’s internal buffer?
I’m writing a file filter that needs to read multi-gigabyte files in small chunks and I’d like to experiment with different buffer sizes to see if it affects performance.
You can use
ios::rdbuf()to get a pointer to astreambufobject. This object represents the internal buffer for the stream.You can call
streambuf::pubsetbuf(char * s, streamsize n)to set a new internal buffer with a given size.See this link for more details.
edit: Here is how it would look in your case:
edit: as pointed out by litb, the actual behavior of
streambuf::pubsetbufis ‘implementation-defined’.If you really want to play around with the buffers, you may have to roll your own buffering class that inherits from
streambuf.