KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | List of all members
kjb::Temporary_File Class Reference

This class safely opens a temporary file in TEMP_DIR (usually /tmp). More...

#include <l_stdio_wrap.h>

Inheritance diagram for kjb::Temporary_File:
kjb::File_Ptr

Public Member Functions

 Temporary_File ()
 
int close ()
 close file before destruction; rarely needed; safe to do twice. More...
 
const std::string & get_filename () const
 
- Public Member Functions inherited from kjb::File_Ptr
virtual ~File_Ptr ()
 dtor can simply call close() because closing a null ptr is OK. More...
 
 operator FILE * () const
 transparently use this object wherever a FILE* is used! More...
 

Additional Inherited Members

- Protected Member Functions inherited from kjb::File_Ptr
void throw_io_error (const std::string &pathname, const char *mode)
 this is what we do when things go terribly wrong More...
 
 File_Ptr (const std::string &pathname, const char *mode)
 ctor needs the pathname and a mode; mode is set by derived class More...
 
 File_Ptr ()
 default ctor wraps a null pointer (to set up a later swap maybe) More...
 
void swap (File_Ptr &that)
 swap the contents of two wrappers More...
 

Detailed Description

This class safely opens a temporary file in TEMP_DIR (usually /tmp).

This class is a wrapper around Unix system call mkstemp which securely generates a unique temporary filename and opens the file too, so that there is no race condition.

This object deletes its underlying file only when the object is destroyed. If the program aborts, the temporary file might not be deleted. In that case, you might want to clean up files called /tmp/libkjb-XXXXXX manually, where XXXXXX denotes any arbitrary combination of six printable characters.

Example usage:

char buf[ 100 ];
fputs( "A is for Absinthe", tfo );
rewind( tfo );
fgets( buf, sizeof(buf), tfo );
puts( buf );

Constructor & Destructor Documentation

kjb::Temporary_File::Temporary_File ( )
inline

Member Function Documentation

int kjb::Temporary_File::close ( )
inlinevirtual

close file before destruction; rarely needed; safe to do twice.

Reimplemented from kjb::File_Ptr.

const std::string& kjb::Temporary_File::get_filename ( ) const
inline

The documentation for this class was generated from the following file: