Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <b>mktemp() create a unique file name</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
mktemp()                 Create a Unique File Name

 #include   <io.h>                       Required for declarations only

 char       *mktemp(template);
 char       *template;                   File name pattern

    mktemp() creates a unique file name by modifying 'template'.
    'template' has the form 'baseXXXXXX'. 'base' is the two-character
    part of the new file name supplied by the user.  The six Xs are
    placeholders for the part supplied by mktemp().  'base' is saved and
    the six trailing Xs are replaced with an alphanumeric character
    followed by a unique five-digit number.

    Returns:    A pointer to the modified template, if successful; or
                null value if 'template' is badly formed or no more
                unique names can be created.

      Notes:    mktemp() creates unique file names. It does not create or
                open files.

                In subsequent calls from the same program with the same
                template, mktemp() checks to see if previously returned
                names are now being used as filenames; if no file exists
                for a given name, mktemp() returns that name. If files
                exist for all previously returned names, mktemp() creates
                a new name by replacing the alphanumeric character in the
                name with the next available lowercase letter. So if the
                first name returned is h0987654, and this name exists as
                a file the next time mktemp() is called, ha987654 will be
                returned. When creating names, mktemp() uses '0' and the
                letters 'a' to 'z', in that order.

  -------------------------------- Example ---------------------------------

    The following statements use mktemp() to create a unique file name
    and then use this name to open a new file.

          #include <io.h>
          #include <fcntl.h>
          #include <sys\types.h>
          #include <sys\stat.h>

          char *template = "nfXXXXXX";
          char *fname;
          int fhndl;

          main()
          {
                fname = mktemp(template);
                fhndl = open(fname,O_CREAT,S_IREAD|S_IWRITE);
                close(fhndl);
          }

See Also: fopen() getpid() open()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson