views:

355

answers:

1

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.

+2  A: 
e.James
Isn't it better to use 'sizeof(myBuffer)' in the call to pubsetbug?
Jonathan Leffler
i've looked it up in the standard, because i wasn't aware what pubsetbuf does, and it says it is implementation-defined what it does. except that pubsetbuf(0, 0) before any I/O will disable buffering
Johannes Schaub - litb
@Jonathan Leffler: Yes, you are right. I have changed it.
e.James
@litb: Lovely! From now on, I'm going to label all of my interfaces as "implementation-defined". That way, I won't have to actually do any work :)
e.James