Popular Tutorials
Start Learning C++Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
C++ tmpfile()
The tmpfile() function in C++ creates and opens a temporary file in binary read/write (wb+) mode with a unique auto-generated filename.
The file will be automatically deleted when it is closed by the program(by executing fclose) or when the program terminates.
tmpfile() prototype
FILE* tmpfile();
It is defined in <cstdio> header file.
tmpfile() Parameters
None
tmpfile() Return value
- If successful, it returns a stream pointer to the temporary file created.
- On failure, it returns null.
Example: How tmpfile() function works
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
FILE *fp;
fp = tmpfile();
char writeToFile[50] = "Creating a temporary file";
if (fp==NULL)
{
perror("Error creating temporary file");
exit(1);
}
fputs(writeToFile,fp);
rewind(fp);
char readFromFile[50];
fgets(readFromFile, sizeof(readFromFile), fp);
cout << readFromFile;
fclose(fp);
return 0;
}
When you run the program, the output will be:
Creating a temporary file
Did you find this article helpful?