c++paginationfstream

multiple access to file in r/w


I'm planning to write a programm which has to access to a certain file many times in r/w. So I decided to use fstream, since I can use this class for both reading and writing purpose.

My idea is to open the file at the startup of the application and then close it as the application is closed too.

Since the file can be arbitrarily big, I was planning to use a "paging" structure, in which:

1) preallocate a fixed amount of memory for each page and a fixed number of page

2) load part of the file in to the first free page

3) if there is no free page, I select one non empty with a certain criterion, I commit all edit in it (if there are any) and then load the part of file in the page.

That's not so hard to code. But I was wondering If I'm going to reinvent the wheel... maybe the fstream itself is written in a smart way so that it also implements a similar paging mechanism. In that case, I would not take care about, just write and read at any time.

Some suggestion?


Solution

  • Don't do this by yourself. Unless you are using very exotic implementation, the fstream class already implement such a mechanism efficiently.

    Checkout http://www.cplusplus.com/doc/tutorial/files/ "Buffers and Synchronization"

    There are possible issues if you are seek-ing into file larger than 2GB with a old kernel or implementation of the standard library. Check this Large file support in C++ or use Boost.Filesystem