Mar 24, 2013 at 1:50pm UTC
As far as I know, std::tmpfile
is the only way to get both atomic unique name generation / file opening and auto-deletion.
I would write a streambuf wrapper, it's a very legitimate case for one. Or, to write less code, you could use fileno() (on a POSIX system) or whatever Windows has to get a HANDLE from FILE*, and then construct a boost.iostreams file_descriptor stream.
Mar 24, 2013 at 11:54pm UTC
Thanks for the links and help, but I'm giving up.
I had begun writing a TmpFileSb class, but I stopped shortly after, because of the overwhelming feeling that I was doing someone else's work .
Mar 30, 2013 at 5:36pm UTC
Last edited on Mar 30, 2013 at 5:36pm UTC
Mar 30, 2013 at 8:03pm UTC
Thanks Duoas. I'll need a good tmpfile() component when/if I attempt the Burrows-Wheeler Transform, a topic for another article.
Mar 31, 2013 at 5:09pm UTC
libstdc++ specific (AFAIK, works only for char and wchar_t):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <cstdio>
#include <iostream>
#include <string>
#include <ext/stdio_filebuf.h> // libstdc++ specific
int main()
{
__gnu_cxx::stdio_filebuf<char > tmpfile_buf( std::tmpfile(),
std::ios::in|std::ios::out|std::ios::binary ) ;
std::iostream temp_fstream( &tmpfile_buf ) ;
// use temp_fstream
temp_fstream << 12345 << ' ' << 67.89 << "Hello World\n" ;
temp_fstream.seekg(0) ;
int i = 0 ; temp_fstream >> i ;
double d = 0 ; temp_fstream >> d ;
std::string str ; std::getline( temp_fstream, str ) ;
std::cout << i << '\n' << d << '\n' << str << '\n' ;
}
Portable:
boost::iostreams::file_descriptor
with
fileno()
(POSIX) or
_fileno()
(Windows)
Last edited on Mar 31, 2013 at 5:14pm UTC