4

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.

1 Answer 1

5

You can use ios::rdbuf() to get a pointer to a streambuf object. 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:


#include <iostream>
#include <fstream>
using namespace std;

int main(int argCount, char ** argList[])
{
    ifstream inStream(argList[1]);

    char myBuffer [512];
    inStream.rdbuf()->pubsetbuf(myBuffer, sizeof(myBuffer));

    char ch;
    while(inStream.read(&ch, 1))
    {
        cout << ch;
    }
}

edit: as pointed out by litb, the actual behavior of streambuf::pubsetbuf is "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.

3
  • Isn't it better to use 'sizeof(myBuffer)' in the call to pubsetbug? Dec 9, 2008 at 2:39
  • 2
    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 Dec 9, 2008 at 2:48
  • @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
    Dec 9, 2008 at 2:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.