C++ way of setting file position for file sizes larger than 32-bits?

topic posted Tue, November 7, 2006 - 11:48 AM by 
Share/Save/Bookmark
Advertisement

Hello, all ...

I need to create a file and set it to a specific size (the idea being that there is another thread that uses std::ofstream::seekp() to write blocks to the file as they arrive off of the network, likely out of order, so if the 10th block of the file arrives before the 1st, I need to be able to handle it). The thing is, std::ofstream::seekp() doesn't seem to allow me to seek past the end of the file and set the file position there e.g. it sets the failbit true if I try. In C, I used to do this with feek(), so I could code something like this:

fseek( pFile, 10000000, SEEK_SET ); // makes the file 10000000 bytes long if it was shorter before

... but fseek() expects a long for the seek position, and this is 2006, where a long isn't big enough (files could be much larger than 4G). I have to do this in a portable way (Windows and Mac OSX / *nix).

Ideas?

Regards,

John, looking for a simple solution

Falling You - exploring the beauty of voice and sound
www.fallingyou.com
posted by:
Advertisement
Advertisement
  • I can't speak for other platforms but there is fsetpos() which I think is standard. I don't know that it does a clear and you may be on your own for that.

    This is Windows specific but when dealing with huge files under Windows I use MapViewOfFile()/UnmapViewofFile() which memory map a window into your file in multiples of SYSTEM_INFO.dwAllocationGranularity using the system's virtual memory functions.