tmpnam() Function
The tmpnam()
function in C stdio.h generates a unique temporary filename that does not conflict with any existing file. It is useful for safely creating temporary files without the risk of overwriting an existing file.
Syntax of tmpnam()
char *tmpnam(char *str);
Parameters
Parameter | Description |
---|---|
str | A pointer to an array of characters where the generated temporary filename will be stored. The array should have a size of at least L_tmpnam characters. Alternatively, a null pointer can be provided to use an internal static array. |
The filename generated by tmpnam()
can be used to create a temporary file using functions like fopen()
. Unlike files created with tmpfile()
, files created using a temporary filename are not automatically deleted when closed; you must explicitly delete them using remove()
.
Return Value
On success, tmpnam()
returns a pointer to the C string containing the temporary filename. If str
was a null pointer, this pointer refers to an internal static buffer; otherwise, it returns the same pointer passed in. If the function fails to generate a filename, it returns a null pointer.
Examples for tmpnam()
Example 1: Generating a Temporary Filename Using an Internal Buffer
This example demonstrates how to generate a temporary filename by passing a null pointer to tmpnam()
, which uses an internal static buffer.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
char *tmpName;
tmpName = tmpnam(NULL);
if (tmpName != NULL) {
printf("Temporary filename: %s\n", tmpName);
} else {
printf("Failed to generate temporary filename.\n");
}
return 0;
}
Explanation:
- The program calls
tmpnam()
with a null pointer to use an internal static buffer. - If a filename is successfully generated, it is printed to the console.
- If the generation fails, an error message is displayed.
Program Output:
Temporary filename: /tmp/xyz123
Example 2: Generating a Temporary Filename Using a User-Provided Buffer
This example shows how to generate a temporary filename by providing a pre-allocated buffer to tmpnam()
.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
char buffer[L_tmpnam];
if (tmpnam(buffer) != NULL) {
printf("Temporary filename using buffer: %s\n", buffer);
} else {
printf("Failed to generate temporary filename.\n");
}
return 0;
}
Explanation:
- A character array
buffer
of sizeL_tmpnam
is declared. tmpnam()
is called withbuffer
to store the generated filename.- If successful, the temporary filename is printed; otherwise, an error message is displayed.
Program Output:
Temporary filename using buffer: /tmp/abc456
Example 3: Creating and Deleting a Temporary File Using the Generated Filename
This example demonstrates how to use the generated temporary filename to create a file, write data to it, and then delete it using remove()
.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
char *tmpName;
FILE *fp;
tmpName = tmpnam(NULL);
if (tmpName == NULL) {
printf("Failed to generate temporary filename.\n");
return 1;
}
fp = fopen(tmpName, "w");
if (fp == NULL) {
printf("Failed to create temporary file.\n");
return 1;
}
fprintf(fp, "This is a temporary file.\n");
fclose(fp);
printf("Temporary file created: %s\n", tmpName);
if (remove(tmpName) == 0) {
printf("Temporary file deleted successfully.\n");
} else {
printf("Failed to delete temporary file.\n");
}
return 0;
}
Explanation:
tmpnam()
is called to generate a temporary filename using an internal static buffer.- The program attempts to create a temporary file with
fopen()
in write mode using the generated filename. - If the file is successfully created, data is written to it and the file is closed.
- The temporary filename is printed, and the file is then deleted using
remove()
. - Finally, a message is printed indicating whether the deletion was successful.
Program Output:
Temporary file created: /tmp/def789
Temporary file deleted successfully.