Results 1 to 11 of 11

Thread: [QT3] Temporary Folder Temporary File

  1. #1
    Join Date
    Oct 2006
    Posts
    5
    Qt products
    Qt3

    Default [QT3] Temporary Folder Temporary File

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT3] Temporary Folder Temporary File

    Maybe you could use tmpfile(3) together with QFile::open()?

  3. #3
    Join Date
    Oct 2006
    Posts
    5
    Qt products
    Qt3

    Default Re: [QT3] Temporary Folder Temporary File

    Quote Originally Posted by jacek View Post
    Maybe you could use tmpfile(3) together with QFile:pen()?
    What do you mean? I don' t understand what is tmpfile(3).

    Thank U

  4. #4
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT3] Temporary Folder Temporary File

    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.

  5. #5
    Join Date
    Oct 2006
    Posts
    5
    Qt products
    Qt3

    Default Re: [QT3] Temporary Folder Temporary File

    Woua !!! I do not manage to follow you !
    is tempfile(3) a Qt function ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT3] Temporary Folder Temporary File

    Quote Originally Posted by Senpai View Post
    is tempfile(3) a Qt function ?
    No, but it's part of standard C library.

  7. #7
    Join Date
    Oct 2006
    Posts
    5
    Qt products
    Qt3

    Default Re: [QT3] Temporary Folder Temporary File

    So, I suppose you are dealing with tmpfile function ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT3] Temporary Folder Temporary File

    Quote Originally Posted by Senpai View Post
    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().

  9. #9
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT3] Temporary Folder Temporary File

    tmpnam just finds a safe temp filename for you

    Qt Code:
    1. #include <stdio.h>
    2.  
    3. int main ()
    4. {
    5. char buffer [L_tmpnam];
    6. char * pointer;
    7.  
    8. tmpnam (buffer);
    9. printf ("Tempname #1: %s\n",buffer);
    10.  
    11. pointer = tmpnam (NULL);
    12. printf ("Tempname #2: %s\n",pointer);
    13.  
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Oct 2006
    Posts
    5
    Qt products
    Qt3

    Default Re: [QT3] Temporary Folder Temporary File

    Using the tmpfile function should be a solution but I wonder where this temporary file is created.

  11. #11
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT3] Temporary Folder Temporary File

    ... 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

    Qt Code:
    1. #include <stdlib.h>
    2. #include <qfile.h>
    3.  
    4. int main ()
    5. {
    6. const char* msg = "inside temp file";
    7. char nameTemplate[] = "/tmp/mytempfile-XXXXXX";
    8.  
    9. QFile f;
    10. int file = mkstemp(nameTemplate) ;
    11. f.open( IO_WriteOnly,file );
    12. f.writeBlock( msg, qstrlen(msg) );
    13. f.close();
    14.  
    15. return 0;
    16. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.