Namespaces
Variants
Views
Actions

tmpnam

From cppreference.com
< c‎ | io
Revision as of 21:36, 31 May 2013 by P12bot (Talk | contribs)

Defined in header <stdio.h>
char *tmpnam( char *filename );

Creates an unique filename and stores it in character string pointed to by filename. The function is capable of generating up to Template:rlpt of unique filenames, but some or all of them may be in use in the filesystem and thus not suitable return values.

Contents

Parameters

filename - pointer to the character string to be used as a result buffer. If NULL is passed, a pointer to an internal static buffer is returned.

Return value

filename if filename was not NULL. Otherwise a pointer to an internal static buffer is returned. If no suitable filename can be generated, NULL is returned.

Example

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
 
int main(int argc, char *argv[])
{
    printf("Welcome to %s\n", argv[0]);
    printf("Called with %u arguments\n", argc - 1);
 
    char buffer[L_tmpnam] = {'\0'};
    tmpnam(buffer);
    printf(buffer);
    printf("\n");
 
    printf("Goodbye!\n");
    exit(EXIT_SUCCESS);
}

Output:

Welcome to ./main_release
Called with 0 arguments
/tmp/file6HADua
Goodbye!

See also

returns a pointer to a temporary file
(function) [edit]