Topic: Accessor functions for fstream


Author: Joe Gottman <jgottman@carolina.rr.com>
Date: Wed, 10 Oct 2007 21:55:52 CST
Raw View
   With move semantics being added to C++0x, it will soon be much easier
to write a function that returns an fstream. But in order to safely use
an object that was returned by a function it is necessary to be able to
examine its state, for example in an assert statement to make sure that
preconditions are met. Unfortunately, fstreams have a lot of state that
is completely inaccessible.  Most obviously, there's no way to determine
the filename that an fstream is reading from or writing to.  Nor is
there any way to determine the mode that the file was opened in.  This
is annoying because, for example, writing to a binary file has different
effects than writing to a text file.

    I think that fstream (and ifstream and ofstream) should have the
following accessor functions:

    const string &file_name() const;
    ios_base::openmode  open_mode() const;

I'm not sure what type the file_name function should return; it could be
a const char *, a string, or a tr2::sys::path (see
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1975.html).  In
any event, this should return the name of the file the stream was opened
for if the stream is open and an empty string otherwise.  Similarly,
open_mode() function should return the mode in which the stream was
opened if the stream is open and 0 otherwise.

Joe Gottman

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]