tmpfile() Function

The tmpfile() function in C stdio.h creates a temporary binary file that is open for update in “wb+” mode. The file is given a unique name, ensuring it does not conflict with any existing file, and it is automatically removed when it is closed or when the program terminates normally.


Syntax of tmpfile()

</>
Copy
FILE *tmpfile(void);

Parameters

This function does not take any parameters.

The temporary file is created in binary mode for update (“wb+” mode) and is guaranteed to have a unique filename. It is automatically deleted when the stream is closed using fclose() or when the program terminates normally.


Return Value

If successful, tmpfile() returns a pointer to the temporary file stream. On failure, it returns NULL.


Examples for tmpfile()

Example 1: Creating and Writing to a Temporary File

This example demonstrates how to create a temporary file using tmpfile() and write data into it.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = tmpfile();
    if (fp == NULL) {
        perror("Failed to create temporary file");
        return 1;
    }

    fprintf(fp, "Temporary file example.");
    rewind(fp);

    char buffer[50] = {0};
    fread(buffer, sizeof(char), 25, fp);
    printf("Data in temporary file: %s\n", buffer);

    fclose(fp); // The temporary file is automatically deleted
    return 0;
}

Explanation:

  1. tmpfile() is called to create a temporary file, and the file pointer is checked for NULL.
  2. Data is written to the temporary file using fprintf().
  3. The file pointer is rewound to the beginning using rewind() to enable reading.
  4. Data is read from the file into a buffer using fread() and printed.
  5. The file is closed with fclose(), which automatically deletes the temporary file.

Program Output:

Data in temporary file: Temporary file example.

Example 2: Reading from a Temporary File

This example shows how to write a string to a temporary file and then read it character by character.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = tmpfile();
    if (fp == NULL) {
        perror("Error creating temporary file");
        return 1;
    }

    fputs("Hello, Temporary File!", fp);
    rewind(fp);

    int ch;
    while ((ch = fgetc(fp)) != EOF) {
        putchar(ch);
    }
    putchar('\n');

    fclose(fp); // Automatically deletes the file
    return 0;
}

Explanation:

  1. A temporary file is created with tmpfile() and the file pointer is verified.
  2. The string "Hello, Temporary File!" is written to the file using fputs().
  3. The file pointer is reset to the beginning using rewind() to allow reading.
  4. The program reads and prints each character from the file using fgetc().
  5. The file is closed with fclose(), which results in the deletion of the temporary file.

Example 3: Handling Temporary File Creation Failure

This example illustrates how to handle a failure when tmpfile() is unable to create a temporary file.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = tmpfile();
    if (fp == NULL) {
        perror("Unable to create temporary file");
        return 1;
    }
    
    // Temporary file created successfully; use it as needed
    fputs("Testing temporary file error handling.", fp);
    
    fclose(fp); // File deletion upon closing
    return 0;
}

Explanation:

  1. The program attempts to create a temporary file using tmpfile().
  2. If the function returns NULL, an error message is displayed using perror() and the program exits with an error code.
  3. If the file is created successfully, it is used to write a test string.
  4. The file is closed with fclose(), which deletes the temporary file.

Program Output:

Testing temporary file error handling.