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()
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
#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:
tmpfile()is called to create a temporary file, and the file pointer is checked forNULL.- Data is written to the temporary file using
fprintf(). - The file pointer is rewound to the beginning using
rewind()to enable reading. - Data is read from the file into a buffer using
fread()and printed. - 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
#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:
- A temporary file is created with
tmpfile()and the file pointer is verified. - The string
"Hello, Temporary File!"is written to the file usingfputs(). - The file pointer is reset to the beginning using
rewind()to allow reading. - The program reads and prints each character from the file using
fgetc(). - 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
#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:
- The program attempts to create a temporary file using
tmpfile(). - If the function returns
NULL, an error message is displayed usingperror()and the program exits with an error code. - If the file is created successfully, it is used to write a test string.
- The file is closed with
fclose(), which deletes the temporary file.
Program Output:
Testing temporary file error handling.
