Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::fstream with multiple buffers?

You can specify one buffer for your file stream like that:

char buf[BUFFER_SIZE];

std::ofstream file("file", std::ios_base::binary | std::ios_base::out);
if (file.is_open())
{
    file.rdbuf()->pubsetbuf(buf, BUFFER_SIZE);
    file << "abcd";
}

What I want to do now, is using more than just one buffer:

char* buf[] = { new char[BUFFER_SIZE], new char[BUFFER_SIZE], new char[BUFFER_SIZE], };

Is it possible without creating a custom derivation of std::streambuf?

EDIT: I think I need to explain what I want to do in more detail. Please consider the following situation: - The file(s) I want to read won't fit into memory - The file while be accessed by some kind of a binary jump search

So, if you split the file into logical pages of a specific size, then I would like to provide multiple buffers which are representing specific pages. This would increase performance when a file location is read and the related page is already in a buffer.

like image 878
0xbadf00d Avatar asked Dec 31 '25 12:12

0xbadf00d


1 Answers

I gather from the comment that you want to do a kind of scatter-gather I/O. I'm pretty sure there's no support for that in the C++ standard I/O streams library, so you'll have to roll your own.

If you want to do this efficiently, you can use OS support for scatter-gather. E.g., POSIX/Unix-like systems have writev for this purpose.

like image 144
Fred Foo Avatar answered Jan 03 '26 02:01

Fred Foo



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!