C++ Program to Create a Temporary File Last Updated : 31 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Here, we will see how to create a temporary file using a C++ program. Temporary file in C++ can be created using the tmpfile() method defined in the <cstdio> header file. The temporary file created has a unique auto-generated filename. The file created is opened in binary mode and has access mode "wb+". These temporary files are automatically deleted when the program is terminated or when they are closed in the program using fclose(). Syntax: std::FILE* tmpfile(); Return value: The associated file stream or a null pointer if an error has occurred. Below is the C++ program to create a temporary file, writing in and reading from the temporary file: C++ // C++ program to create a temporary file // read and write to a temporary file. #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; // Driver code int main() { // Creating a file pointer which // points to the temporary file // created by tmpfile() method FILE* fp = tmpfile(); // Content to be written in temporary file char write[] = "Welcome to Geeks For Geeks"; // If file pointer is NULL there is // error in creating the file if (fp == NULL) { perror("Error creating temporary file"); exit(1); } // Writing in temporary file the content fputs(write, fp); rewind(fp); // Reading content from temporary file // and displaying it char read[100]; fgets(read, sizeof(read), fp); cout << read; // Closing the file. Temporary file will // also be deleted here fclose(fp); return 0; } Output: Welcome to Geeks For Geeks C++ Program to Create a Temporary File Comment More info I ishankhandelwals Follow Improve Article Tags : C++ Programs C++ C++ File Programs Explore C++ BasicsIntroduction to C++ Programming Language3 min readData Types in C++7 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++10 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library2 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples7 min read Like