Joe said:
Hi,
I want to use the C library function mkstemp. It returns a unix file
descriptor (int) to an open file. How do I convert the file descriptor to a
valid ofstream object?
Thanks,
Joe
In the GCC tool chain, the following member header occurs in fstream:
ofstream(int fd) : fstreambase(fd) { }
this means that you could write something like:
#include <fstream>
#include <stdlib.h>
#include <string.h>
int main(){
char *f_template;
f_template = new char(12);
memcpy(f_template, "/tmp/XXXXXX", 12);
int fd = mkstemp(f_template);
cout << "File Descriptor # is: " << fd << " file name = " <<
f_template <<endl;
ofstream my_temp_out(fd);
my_temp_out << "Some test text" << endl;
while(1){} // Now take a look in your temp directory for the file
//name printed from the above cout statement. If you cat it out,
//you should see the "test text".
return 0;
}