Creating a Temporary Filename and File - C++ File Stream

C++ examples for File Stream:File Operation

Description

Creating a Temporary Filename and File

Demo Code

#include <iostream>
#include <cstdio>

using namespace std;

int main() {/*w w  w  . j  ava 2  s.  c  om*/
   FILE* pf = NULL;
   char buf[256];

   pf = tmpfile();  // Create and open a temp file

   if (pf) {
      fputs("This is a temp file", pf);  // Write some data to it

   }

   fseek(pf, 5, SEEK_SET);  // Reset the file position
   fgets(buf, 255, pf);     // Read a string from it
   fclose(pf);

   cout << buf << '\n';
}

Result


Related Tutorials