std::ofstream filename

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • red floyd

    std::ofstream filename


    Is there any way to retrieve the filename given to a std::ofstream
    (passed in constructor or in ofstream::open( ))?

    Or, should I derive from ofstream (should probably be a template to
    handle ifstream and fstream as well, but I'm typing on the fly)

    yes, I know the syntax may be off...


    class onamedfstream : public ofstream {
    private:
    std::string filename_;
    public:
    ofnamedstream() : ofstream() { }
    ofnamedstream(c onst char * fname, ios_base::openm ode mode =
    ios_base::out) :
    filename_(fname ), ofstream(fname, mode) { }
    ~ofnamedstream( ) { }
    void open(const char *fname, ios_base::openm ode = ios_base::out)
    {
    filename_ = fname;
    ofstream::open( fname, mode);
    }
    const std::string& get_filename(vo id) const { return filename_; }
    };

  • Thomas Matthews

    #2
    Re: std::ofstream filename

    red floyd wrote:[color=blue]
    >
    > Is there any way to retrieve the filename given to a std::ofstream
    > (passed in constructor or in ofstream::open( ))?
    >
    > Or, should I derive from ofstream (should probably be a template to
    > handle ifstream and fstream as well, but I'm typing on the fly)
    >
    > yes, I know the syntax may be off...
    >
    >
    > class onamedfstream : public ofstream {
    > private:
    > std::string filename_;
    > public:
    > ofnamedstream() : ofstream() { }
    > ofnamedstream(c onst char * fname, ios_base::openm ode mode =
    > ios_base::out) :
    > filename_(fname ), ofstream(fname, mode) { }
    > ~ofnamedstream( ) { }
    > void open(const char *fname, ios_base::openm ode = ios_base::out)
    > {
    > filename_ = fname;
    > ofstream::open( fname, mode);
    > }
    > const std::string& get_filename(vo id) const { return filename_; }
    > };
    >[/color]

    There are no _standard_ methods in the iostream library for
    retrieving the filename after the stream has been opened.

    Most programmers remember the filename by using some sort of
    variable or string literal.

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    Working...