Accessing the UNIX file descriptor inside an fstream
Chris Rankin
rankincj@yahoo.com
Wed Sep 19 16:16:00 GMT 2001
I have modified your example program to represent more
accurately what I was trying to do. This version
core-dumps, of course, due to destructor issues. Looks
like "catch 22" to me, because if I could create the
filebuf within the initialiser list then I wouldn't
need it in the first place.
Cheers,
Chris
-----------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <fcntl.h>
using namespace std;
class Bar
{
private:
ofstream m_file;
public:
Bar();
void write(const string &msg);
};
Bar::Bar()
{
int fd = ::open("fooey.dat",
O_CREAT | O_WRONLY | O_APPEND,
S_IRUSR | S_IWUSR | S_IRGRP |
S_IROTH);
if (fd == -1)
{
cerr << "Error creating descriptor: "
<< strerror(errno)
<< endl;
}
else if ( fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 )
{
cerr << "Error setting descriptor flag: "
<< strerror(errno)
<< endl;
}
else
{
FILE *newFile = fdopen(fd, "a");
if (!newFile)
{
cerr << "Error creating new stream: "
<< strerror(errno)
<< endl;
}
else
{
filebuf buf(newFile, ios_base::out |
ios_base::app);
m_file.basic_ios<char>::rdbuf(&buf);
write("This should appear...");
}
}
}
void
Bar::write(const string &msg)
{
m_file << msg << endl;
}
int
main()
{
Bar bar;
bar.write("This should core-dump!");
return 0;
}
__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/
More information about the Gcc-bugs
mailing list