Namespaces
Variants
Views
Actions

tmpfile

From cppreference.com
< c‎ | io
Revision as of 04:31, 2 May 2014 by Newatthis (Talk | contribs)

Defined in header <stdio.h>
FILE *tmpfile();

Opens a temporary file. The file is opened as binary file for update ("wb+ mode). The filename of the file is guaranteed to be unique within the filesystem.

The file will be closed when the program exits.

Contents

Parameters

(none)

Return value

The associated file stream or NULL if an error has occurred

Example

tmpfile with error checking. Code opens a temporary file with mode "wb+".

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    FILE* tmpf = tmpfile();   /* mode: "wb+" */
    if (tmpf == NULL)
    {
       perror("tmpfile()");
       fprintf(stderr,"tmpfile() failed in file %s at line # %d", __FILE__,__LINE__-4);
       exit(EXIT_FAILURE);
    }
    fputs("Hello, world", tmpf);
    rewind(tmpf);
    char buf[6];
    fgets(buf, sizeof buf, tmpf);
    printf("%s\n", buf);
 
    return EXIT_SUCCESS;
}

Output:

Hello

See also

returns a unique filename
(function) [edit]
C++ documentation for tmpfile