tmpfile()

Declaration

FILE* tmpfile();

Description:

This function creates and opens a temporary file for reading and writing in binary mode. The function returns a pointer to the opened file, if successful and NULL if an error occurs. The file is automatically deleted when it is closed or the program terminates.

Example

#include <cstdio>

int main()
{
    // Open a temp file in binary mode.
    FILE* qpFile = tmpfile();
    if (!qpFile) {
        perror("Could not open temp file:");
        return 1;
    }
    char caWrite[] = "XoaX.net";
    char caRead[20];
    int iSize = sizeof(caWrite);

    // A binary write and read
    fwrite(caWrite, 1, iSize, qpFile);
    rewind(qpFile);
    fread(caRead, 1, iSize, qpFile);

    // Add null-terminator before we output
    caRead[iSize] = '';
    // Output the read contents
    printf("Read: %s\n", caRead);

    fclose(qpFile);

    return 0;
}

Output:

tmpfile() Output