View Full Version : QTemporaryFile: unique filename across platforms?

26th December 2010, 15:54

Are the filenames used by QTemporaryFile unique across platforms? E.g., two files created in the same directory in Linux on a USB stick must not be named FILE1.txt and file1.txt, as this would create problems when the USB stick is used on a Windows machine.


I have a database where files can be added: the file content is copied to a dedicated directory, the file name is stored in the database together with tags. As over time the same filename could be added (e.g., img00001.jpg is a common filename ...), I need to rename the files when copying to the dedicated directory. And I consider to use QTemporaryFile to create unique filenames.

26th December 2010, 20:46
Typically, if you wish to use the same USB stick across OSs such as Windows and Linux, you would not be using a case sensitive file system such as Ext2 anyway, but rather a case insensitive one like VFAT, so it doesn't really matter if linux calls a file "file1.txt", it would still be called FILE1.TXT on Windows, and if Windows creates a file called IMAGE1.JPG, it could be accessed as image1.jpg under Linux.

(Of course, Windows can use LFNs to state the case of a file rather than uppercasing it, but the result is the same)

26th December 2010, 21:21
Thanks. So, QTemporaryFile should do the work as long as I create the files from the beginning on a USB stick (FAT32 formatted).

What if I create the directory on a local harddisk (ext3 formatted) and later copy that directory plus the database (using a USB stick or the like) to another computer running Windows? Would QTemporaryFile still guarantee that no conflict emerges?

26th December 2010, 23:20
If the numbers used in the filename are unique, the filename will be unique.

The only problem you have is if you have two files that only differ in character case. What will happen then is that the second file will overwrite the first, thus you must ensure that will never happen.

27th December 2010, 01:43

You could use a QUuid::toString() as filename if you trim the bracelets. This is guaranteed to be unique.


27th December 2010, 19:04
Thanks to marcvanriet and squidge for their suggestions. Both are valid, both have some minor drawbacks: if using QTemporaryFile I need to check for file names differing only in capitalization; QUuid (as I understand the Qt documentation) is on Linux not truly guaranteed to be unique (although the same uuid is very unlikely to be produced twice). Options:

I use QUuid andI live with the remote possibility of failure
I use QTemporaryFile::filename().toLower() and check that this filename is still unique

As a purist, I rather go for option 2 and will try to implement this in a class derived from QTemporaryFile where I re-implement the virtual function bool open(OpenMode flags)

29th December 2010, 08:44
I wrote a small class QXUniqueFile that uses QTemporaryFile in the back to create truly unique file names, see attached (main,cpp and QXUniqueFile.pro are a console application that demonstrates use of the class).