Ofstream filename variable

C

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
 
J

John Harrison

cpp said:
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::eek:pen(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
 
R

RCS

John said:
class ofstream_with_filename : public ofstream
{
public:
void open(const char* fn)
{
ofstream::eek:pen(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
 
D

Duane Hebert

RCS said:
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;}
 
J

John Harrison

RCS said:
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
 
C

Chris Mantoulidis

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? ;)
 

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

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top