By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,010 Members | 2,774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,010 IT Pros & Developers. It's quick & easy.

std::ofstream filename

P: n/a

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(const char * fname, ios_base::openmode mode =
ios_base::out) :
filename_(fname), ofstream(fname, mode) { }
~ofnamedstream() { }
void open(const char *fname, ios_base::openmode = ios_base::out)
{
filename_ = fname;
ofstream::open(fname, mode);
}
const std::string& get_filename(void) const { return filename_; }
};

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
red floyd wrote:

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(const char * fname, ios_base::openmode mode =
ios_base::out) :
filename_(fname), ofstream(fname, mode) { }
~ofnamedstream() { }
void open(const char *fname, ios_base::openmode = ios_base::out)
{
filename_ = fname;
ofstream::open(fname, mode);
}
const std::string& get_filename(void) const { return filename_; }
};


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:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.