Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <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', which
    has the form 'baseXXXXXX'.

    'base' is the two-character part of a new user-supplied file name.
    The six trailing Xs are placeholders, which mktemp() replaces with an
    alphanumeric character and a unique five-digit number.

    When first called by a program, mktemp() uses 0 as the alphanumeric
    character in the filename.  On succeeding calls by the same program,
    using the same template, mktemp() replaces 0 with, in order, the
    lowercase letters of the alphabet.

    After the first call, mktemp() checks whether previously returned
    names are now being used as filenames. If a file has not been created
    for a given name, mktemp() returns that name.  If files have been
    created for all prior filenames, mktemp() creates a new filename, as
    described above.  So, for example, suppose the first name mktemp()
    creates is he098765.  The next time mktemp() is called, this name
    exists as a file.  mktemp() then creates a new filename by replacing
    0 with a yielding hea98765, and so on.

    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.

 Portability:   Not supported by ANSI standard.

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