Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I discover/control the level of internal buffering in a C++ fstream?

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.

like image 306
AndrewR Avatar asked Oct 16 '25 20:10

AndrewR


1 Answers

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.

like image 159
e.James Avatar answered Oct 18 '25 09:10

e.James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!