help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: not able to get fd of ofstream using stdio_filebuf


From: Ulrich Eckhardt
Subject: Re: not able to get fd of ofstream using stdio_filebuf
Date: Mon, 28 Mar 2005 12:26:40 +0200
User-agent: KNode/0.8.1

Devika wrote:
> I want to call fcntl function for which I need fd().
> The code part is smthing like :
> 
> ofstream osf("somefile" , ios::app);
> 
> filebuf * fb ;
> fb = osf.rdbuf();
> 
> fcntl(fb -> fd(), F_SETLKW, &cfLock);
> 
> Now this code was working fine with gcc 2.96 but now it[gcc 3.2.3] says
> no matching function for fd() since the function fd() of basic_filebuf
> has been removed in gcc 3.2.3. and they have provided new extension
> stdio_filebuf.

1. http://gcc.gnu.org/gcc-2.96.html
2. access to underlying filedescriptors was never part of C++ iostreams,
although GCC's C++ stdlibrary provided means for that.

> #include <ext/stdio_filebuf.h>
> typedef __gnu_cxx::stdio_filebuf FILEBUF;
> 
> ofstream osf("somefile" , ios::app);
> FILEBUF *fb ;
> fb = dynamic_cast<FILEBUF *>(osf.rdbuf());
> 
> ...But its not getting typecasted properly and i m getting segment
> fault.

What do you mean it is not getting typecasted properly? Are you aware of
the semantics of dynamic_cast?

Anyhow, I can see two solutions:
1. there is a complementary to stdio_filebuf which is a stream that you
should use instead of plain std::ofstream.
2. you could instantiate stdio_filebuf directly and pass a pointer to an
ostream's ctor. Above assumed stdio_ofstream is probably nothing
different, just that it wraps things up nicely.

BTW, did you even bother reading the intended topic of gnu.gcc.help? Shame
no you.

Uli

-- 
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]