c++file

How to create a temporary text file in C++?


I'm trying to create a temporary text file in C++ and then delete it at the end of the program. I haven't had much luck with Google.

Could you tell me which functions to use?


The answers below tell me how to create a temp file. What if I just want to create a file (tmp.txt) and then delete it? How would I do that?


Solution

  • Maybe this will help

    FILE * tmpfile ( void );
    

    http://www.cplusplus.com/reference/clibrary/cstdio/tmpfile/

    Open a temporary file

    Creates a temporary binary file, open for update (wb+ mode -- see fopen for details). The filename is guaranteed to be different from any other existing file. The temporary file created is automatically deleted when the stream is closed (fclose) or when the program terminates normally.

    See also

    char * tmpnam ( char * str );
    

    Generate temporary filename

    A string containing a filename different from any existing file is generated. This string can be used to create a temporary file without overwriting any other existing file.

    http://www.cplusplus.com/reference/clibrary/cstdio/tmpnam/