PDA

View Full Version : QTemporaryFile and Qfile::copy()



ChrisW67
22nd April 2009, 02:03
Hi Guys,

I have a need to copy an existing file, or resource, into a temporary file. I can use the QTemporaryFile to create an empty temporary file in a safe fashion. However, to use QFile::copy() to move my data I need the target file to not exist. If I grab the fileName() and remove the temp file then I have put myself back into the race condition that tmpnam (3) warns against and that QTemporaryFile is designed to avoid. There does not seem to be a file copy that uses open QFile (QIODevice) as source and target.

Is there an elegant QT approach to this problem?

Cheers,
Chris

Edit: I can use QTemporaryFile::createLocalFile() on the resource to get the copy on to disc, but this will not copy an already local file.

rexi
23rd April 2009, 14:37
Apparently the only way to do it is to manually read the data from the source file and write it to the temporary file using QIODevice's read/write methods.

ChrisW67
23rd April 2009, 23:32
Thanks. That's the conclusion I had reached also.