PDA

View Full Version : Does seeking past the end of a file and writing generate defined behavior?



manekineko
8th August 2011, 18:25
I'm thinking about using QFile and the functions it inherits from QIODevice, seek and write.

I will be receiving data for the file over a network connection, and the data is not guaranteed to come in the proper order, but will come with the offset in the file from which to start writing.

When I first use QFile to create a new file, it is zero bytes. If I then seek to 1024 and write 512 of data, my file should now be 1536 bytes long.

Is this proper usage of seek and write?
Is it defined what will be in the QFile from 0-1024 (i.e. will it reliably 0 those parts)?

wysota
8th August 2011, 19:11
Is this proper usage of seek and write?
If the filesystem handles sparse files (most do, at least Unix ones) then yes. For other filesystems you'll probably get an error.


Is it defined what will be in the QFile from 0-1024 (i.e. will it reliably 0 those parts)?
If the filesystem handles sparse files, those missing chunks will be filled with virtual zeroes. By virtual I mean they won't take space on your disk. For filesystems without support for sparse files should you not get an error while trying to seek past the end of the file, you will probably get a proper file with junk inside skipped blocks.