473,607 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_; }
};

Jul 19 '05 #1
1 11399
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(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_; }
};


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.l earn.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
7813
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 for the screen. Right now the two declarations are: std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass); std::ostream& operator<<( std::ostream &fos, const Foo &theClass);
2
448
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 did below: char filename = {"quest.01.cpp","quest.02.cpp","quest.03.cpp","quest.04.cpp","quest.05.cpp"};
8
14291
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 ifstream. Something I found shows that, I need to have a proper codecvt to set it. I need more information, maybe a small piece of code sample. Thank you!
9
3621
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 << r.getRoomName() << "\n"; 58 outfile << r.getRoomDescription() << "\n"; 59 int i; 60 for(i = 0; i > sizeof(r.getRoomExits()); i++){ 61 if(i = 5){
2
5659
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
3020
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: "output_<currenttimestamp>.txt" thanks.
2
9998
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 set the ifstream to stdin and the ofstream to stdout or stderr? If the answer is yes, please provide a code snippet. Thanks, Masood
2
2363
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
2604
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 to the file is handled through a std::ofstream. The output comes from a 3 x 3 matrix and for testing purposes, each floating point value in the matrix is preceded by the string "value = " when it is output to the stream. As you can see below from...
0
8472
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8464
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8130
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8324
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4015
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2464
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1318
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.