PDA

View Full Version : [QT3] Temporary Folder Temporary File



Senpai
6th October 2006, 08:46
Dear All,

I would like to create a file in a directory where I am sure that the user have writing rights that's why I've choosed the temporary folder of the user. (TEMP, TMPDIR ...)

In QT4 there is a very useful class QtemporaryFile which woul enable me to do this easily but I use QT3 and this class does not exist.

Do you have information which could help me?
In advance Thank U

jacek
6th October 2006, 09:29
Maybe you could use tmpfile(3) together with QFile::open() (http://doc.trolltech.com/3.3/qfile.html#open-2)?

Senpai
6th October 2006, 09:33
Maybe you could use tmpfile(3) together with QFile::open() (http://doc.trolltech.com/3.3/qfile.html#open-2)?

What do you mean? I don' t understand what is tmpfile(3).

Thank U

jrideout
6th October 2006, 09:42
from $man tempfile


NAME
tempfile - create a temporary file in a safe manner

SYNOPSIS
tempfile [-d DIR] [-p STRING] [-s STRING] [-m MODE] [-n FILE] [--directory=DIR] [--prefix=STRING] [--suffix=STRING] [--mode=MODE] [--name=FILE] [--help] [--version]

DESCRIPTION
tempfile creates a temporary file in a safe manner. It uses tempnam(3) to choose the name and opens it with O_RDWR | O_CREAT | O_EXCL. The filename is printed on standard output.

The directory to place the file is searched for in the following order:

a) The directory specified by the environment variable TMPDIR, if it is writable.

b) The directory specified by the --directory argument, if given.

c) The directory /tmp.

Senpai
6th October 2006, 09:44
Woua !!! I do not manage to follow you !
is tempfile(3) a Qt function ?

jacek
6th October 2006, 09:57
is tempfile(3) a Qt function ?
No, but it's part of standard C library.

Senpai
6th October 2006, 10:04
So, I suppose you are dealing with tmpfile function ?

jacek
6th October 2006, 10:16
So, I suppose you are dealing with tmpfile function ?
Yes, you can open a temporary file with tmpfile() and pass returned FILE * to QFile::open().

jrideout
6th October 2006, 10:22
tmpnam just finds a safe temp filename for you



#include <stdio.h>

int main ()
{
char buffer [L_tmpnam];
char * pointer;

tmpnam (buffer);
printf ("Tempname #1: %s\n",buffer);

pointer = tmpnam (NULL);
printf ("Tempname #2: %s\n",pointer);

return 0;
}

Senpai
6th October 2006, 10:45
Using the tmpfile function should be a solution but I wonder where this temporary file is created.

jrideout
6th October 2006, 10:54
... I wonder where this temporary file is created.
That example i showed you prints the location of the file.


This example is more secure, but you have to specify a file name template


#include <stdlib.h>
#include <qfile.h>

int main ()
{
const char* msg = "inside temp file";
char nameTemplate[] = "/tmp/mytempfile-XXXXXX";

QFile f;
int file = mkstemp(nameTemplate) ;
f.open( IO_WriteOnly,file );
f.writeBlock( msg, qstrlen(msg) );
f.close();

return 0;
}