News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

C++ tips:IO - Getting file descriptors from fstream

Started by ben2ong2, October 06, 2006, 11:05:48 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Charlie Morss)

I recently asked a question in regard to record locking using
the fstream class. Steve Clamage replied and suggested that
I derive a class from fstream called lfstream in which I could
then have a lock and unlock member function. Now the question
is how do you get the file descriptor from the fstream class?


RESPONSE: You are not allowed to view links. Register or Login (Steve Clamage)
          Vice Chair, ANSI C++ Committee, X3J16


Check <fstream.h>.  Class filebuf usually has a member function
fd() which returns the file descriptor.

This is all rather specific to Unix-like implementations.  If you
are trying to do file locking on a radically different system,
there may be slightly different functions.  Presumably the filebuf
class provides some way to get at the underlying file in a way
consistent with the file locking routines.

Example:


#include <fstream.h>

main()
{
    fstream f("foo.bar", ios::in|ios::out);

    // use fstream::rdbuf() to get the filebuf
    // then filebuf::fd() to get the file descriptor
    cout << "file descriptor = " << (f.rdbuf())->fd() << endl;

    return 0;
}



You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login