Creating a temporary filename - C++ File Stream

C++ examples for File Stream:File Operation

Description

Creating a temporary filename

Demo Code

#include <iostream>
#include <fstream>
#include <cstdio> 
#include <string> 

using namespace std; 

int main() { /*from  ww  w.j av  a2s.  c  o m*/

   char* pFileName = NULL; 

   pFileName = tmpnam(NULL); 

   if (!pFileName) { 
      cerr << "Couldn't create temp file name.\n"; 
      return(EXIT_FAILURE); 
   } 

   cout << "The temp file name is: " << pFileName << '\n'; 

   ofstream of(pFileName);

   if (of) {
      of << "Here is some temp data.";
      of.close();
   }

   ifstream ifs(pFileName);
   string s;

   if (ifs) {
      ifs >> s;
      cout << "Just read in \"" << s << "\"\n";
      ifs.close();
   }
}

Result


Related Tutorials