473,396 Members | 1,995 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,396 software developers and data experts.

Ofstream filename variable

cpp
When I create an instance of ofstream, what is the name of the member
variable that holds the filename?
For example:

ofstream ofs("Output.txt");
cout << ofs.WhatIsThePathVariable;

If there isn't a public member variable I can use, then is there at least a
function that displays the filename?

Thanks
Jul 22 '05 #1
5 2634

"cpp" <cp*@on.the.net> wrote in message
news:Xn*************************@24.24.2.165...
When I create an instance of ofstream, what is the name of the member
variable that holds the filename?
There is none.
For example:

ofstream ofs("Output.txt");
cout << ofs.WhatIsThePathVariable;

If there isn't a public member variable I can use, then is there at least a function that displays the filename?


No again.

Use a little imagination. Since you opened the file with a file name you
must also be capable of storing that file name somewhere. For instance you
could derive a class from ofstream that stores the filename

class ofstream_with_filename : public ofstream
{
public:
void open(const char* fn)
{
ofstream::open(fn);
filename = fn;
}
string get_filename() const { return filename; }
private:
string filename;
};

I hope that gives you the idea, real code would be a little more
sophisticated than the above.

john
Jul 22 '05 #2
RCS
John Harrison wrote:
class ofstream_with_filename : public ofstream
{
public:
void open(const char* fn)
{
ofstream::open(fn);
filename = fn;
}
string get_filename() const { return filename; }
private:
string filename;
};

I hope that gives you the idea, real code would be a little more
sophisticated than the above.

john


Yes, REAL CODE(tm) would do it like this:

string& get_filename() const { return filename; }

Apart from this, why do you say that "real code" would do it so much
different than your snippet?

Just wondering.

RXX

Jul 22 '05 #3

"RCS" <rc****@online.no> wrote in message news:n4******************@news2.e.nsc.no...
Yes, REAL CODE(tm) would do it like this:

string& get_filename() const { return filename; }


So real code would return a non const reference to
a private member?

How about
const string & get_filename() const { return filename;}
Jul 22 '05 #4

"RCS" <rc****@online.no> wrote in message
news:n4******************@news2.e.nsc.no...
John Harrison wrote:
class ofstream_with_filename : public ofstream
{
public:
void open(const char* fn)
{
ofstream::open(fn);
filename = fn;
}
string get_filename() const { return filename; }
private:
string filename;
};

I hope that gives you the idea, real code would be a little more
sophisticated than the above.

john
Yes, REAL CODE(tm) would do it like this:

string& get_filename() const { return filename; }


No! Three things wrong with that

1) Doesn't compile (non-const reference being taken to const object).
2) Returning reference to internal data member is bad style generally
because it limits your implementation. Suppose you were porting this code to
a O/S where you did have the filename available from some O/S API. How could
you take advantage of that given that you are returning a reference?
3) Most importantly, it's useless functionality. Why would you want to
change the file anme associated with an open file? By return a non-const
reference you are allowing the user to do exactly that.


Apart from this, why do you say that "real code" would do it so much
different than your snippet?

I was thinking of two things, implementing all of ofstream constructors in
ofstream_with_filename, and error handling, what do you do with the filename
if the open fails (leave it unset presumably). But I was too lazy to
actaully implement that.
Just wondering.

RXX


john
Jul 22 '05 #5
RCS <rc****@online.no> wrote in message news:<n4******************@news2.e.nsc.no>...
John Harrison wrote:
class ofstream_with_filename : public ofstream
{
public:
void open(const char* fn)
{
ofstream::open(fn);
filename = fn;
}
string get_filename() const { return filename; }
private:
string filename;
};

I hope that gives you the idea, real code would be a little more
sophisticated than the above.

john
Yes, REAL CODE(tm) would do it like this:

string& get_filename() const { return filename; }

Apart from this, why do you say that "real code" would do it so much
different than your snippet?

Just wondering.

RXX


REAL CODE(tm) would do it like this:

const string& get_filename() const { return filename; }

or else the following would have been possible (with your piece of REAL CODE):

ofstream_with_filename owf;
owf.open("test.txt");
owf.get_filename() = "something";

and we don't want the last to happen now do we? ;)
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: james545 | last post by:
I am having a problem with ofstream creating files. Sometimes when I run the program, ofstream creates a new file for writing and writes to it. No problem. But sometimes it fails to create the...
2
by: Lars Yencken | last post by:
Hello, I'm working on a project where we need to index files as we're writing them, which basically just involves determining how many bytes are output each time we write to a stream. What...
1
by: red floyd | last post by:
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...
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...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
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 <<...
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:...
5
by: mastern200 | last post by:
I need to make a program where it can read from a file and write to it. But the problem is, all i can do is read from it and not write to it. This is the code i have so far: # include <iostream>...
5
by: ednaswap | last post by:
Hello, C++ experts, I am writing a Log class something like below. ================= c++ source code ======================== class Log { public: Log(); Log(string _filename);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.