[Qt-interest] Writing file.
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Wed Apr 8 14:49:59 CEST 2009
Arnt Gulbrandsen wrote on Wednesday, April 08, 2009 1:11 PM:
> offlist
>
> Oliver.Knoll at comit.ch writes:
>> Where do you *close* the file? (Rhetorical question)
>>
>> No close(), no flush, no bytes written!
>
> The file should be closed when filewrite goes out of scope, not?
I did not check the Qt docs right now, but you are probably right: the d'tor of QFile much likely calls close() implicitly.
Still, I consider it good style to /explicitly/ release resources, especially as soon as they are not used anymore. And it makes it clear for the reader that the person who wrote the code actually takes care of releasing resources - also in cases where this is not necessarily done automagically ;)
But back to the original question: even if you call close() this (probably) does *not* imply a flush(). close() merely gives the file descriptor "back to the OS" (which *might* off course influence the caching/writing, since the I/O module of the OS now knows that no one is going to write anything more into that file - at least not this process which just closed the file). In other words, depending on the caching strategy (and other write() operations) it might still take some while until the bytes finally arrive physically on the disk!
For example the Ext4 file system caches small writes up to one (!) minute (unless flush() is called). And even if you call flush(), there's still the physical harddisk controller cache... ;)
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list