DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: FILE or file descriptor for an fstream?

  1. #1
    Join Date
    Oct 2007

    FILE or file descriptor for an fstream?

    Can I get an associated FILE or file descriptor for an fstream object?

    My guess is no, since O'reilly doesn't tell me how and says that basic_filebuf may or may not use stdio.h functions.

    I thought I'd ask the experts before giving up.

  2. #2
    Join Date
    Dec 2003
    What exactly are you wanting to do? I think you can get a descriptor, but not a C FILE structure from a fstream. But what is the goal?

  3. #3
    Join Date
    Nov 2003
    This is a frequent question, and the answer is probably "no". At least with respect to standard C++, there's no way to obtain a descriptor from an fstream object or bind an fstream object to a descriptor. The reason, as your book suggested, is implementation dependecies. Just as new doesn't necessarily have to use malloc() to allocate raw memory, the fstream library isn't required to use stdio.h as its underlying implementation, and in fact, there are quite a few libraries that don't implement fstream by means of stdio.h.
    Non-standard API (mostly in the POSIX) world did have a way to do that though. They are deprecated and non-portable but they will probably live forever, just as io.h does...
    Danny Kalev

  4. #4
    Join Date
    Oct 2007
    What led to the question was my desire to "lock" streams for thread safety: to prevent mixing and matching of lines of output.
    In POSIX.1, all FILE type routines are required to act as though sandwiched between flockfilelock() and flockfileunlock(). Depending on the implementation of ostreams, you might (only hope) to get items between the "<<" to be output "atomically".

    It would be wonderful if the new c++ standards include stream locking.... Will they?

  5. #5
    Join Date
    Nov 2003
    C++ is in the process of adding concurrency support, which includes a thread library, a modified memory model etc. so I assume that streams will also undergo a similar facelift, but at the moment I'm not aware of any concrete proposal.
    Danny Kalev

  6. #6
    Join Date
    Oct 2007
    In the process...
    Does that mean don't expect it in c++09?

  7. #7
    Join Date
    Nov 2003
    Not necessarily. There are concrete C++09 proposals for thread support, and there's also a proposal for thread-local storage, so may get something like a thread-local stream object that can be accesses only from within its thread.
    Most stream implementations are already thread safe to some extent, e.g., Rogue Wave's STL uses locks for I/O.
    Danny Kalev

Similar Threads

  1. Problem compiling a C++ file in Linux
    By kepler in forum C++
    Replies: 3
    Last Post: 08-26-2007, 12:11 AM
  2. Database not properly created
    By Arjuna in forum Database
    Replies: 2
    Last Post: 07-25-2007, 03:22 AM
  3. Replies: 6
    Last Post: 08-16-2006, 10:32 PM
  4. Either Overwrite or Rename file
    By Shaitan00 in forum Java
    Replies: 1
    Last Post: 04-06-2005, 06:04 AM
  5. Test a file to see if it's open
    By JohnN in forum VB Classic
    Replies: 12
    Last Post: 05-08-2001, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center