473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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(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
1 11326
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chase Bradford | last post by:
Hey all I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate formats for output, one for files and another...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
9
by: Felix85 | last post by:
53 void room2File(room r){ 54 ofstream outfile("../gamefiles/rooms/" + r.getRoomNumber() + ".room"); 55 56 outfile << r.getRoomNumber() << "\n"; 57 outfile <<...
2
by: thinktwice | last post by:
//case 1, works fine function(..) { std::ofstream out; out.open(file); out << "error info" <<endl; out.close(); } //case 2, declare it in a class
6
by: newshop | last post by:
How can I make the filename variable, by appending the current timestamp to file, instead of following static file name: ofstream SaveFile("output.txt"); example something like:...
2
by: masood.iqbal | last post by:
In the stdio library, supported by C, we can set a file pointer to stdin or stdout ot stderr (e.g. FILE* fp = stdin;). Can we do something similar with ifstream and ofstream in C++? i.e. can we...
2
by: Dave Johansen | last post by:
Is it ok to call fcloseall() when using both C-style FILE * and C++ std::ofstream's to read files in a program? Thanks, Dave
1
by: meLlamanJefe | last post by:
I have a set of values that need to be output to a text file. The output includes both text and numbers so I'd like to line up the floating point numbers on the list so it is easy to read. The output...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.