std::ofstream filename

R

red floyd

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

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

Thomas Matthews

red said:
Is there any way to retrieve the filename given to a std::eek:fstream
(passed in constructor or in ofstream::eek:pen())?

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::eek:penmode mode =
ios_base::eek:ut) :
filename_(fname), ofstream(fname, mode) { }
~ofnamedstream() { }
void open(const char *fname, ios_base::eek:penmode = ios_base::eek:ut)
{
filename_ = fname;
ofstream::eek:pen(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top