How can I get file name from ostream ?

S

santhosh_h_98

ostream& operator<<(ostream& o, const bdd& d) {
I want to Print file name here. How can I get file name from o ?
}

void main() {
bdd c(a,5,9) ;
ofstream out("junk") ;
out << c ;
}

Santhosh
 
J

John Harrison

ostream& operator<<(ostream& o, const bdd& d) {
I want to Print file name here. How can I get file name from o ?
}

void main() {
bdd c(a,5,9) ;
ofstream out("junk") ;
out << c ;
}

Santhosh

You cannot. For one thing an ostream is not necessarily a file. But even
if you had an fstream, there is no way to get the file name from it.

john
 
V

Victor Bazarov

ostream& operator<<(ostream& o, const bdd& d) {
I want to Print file name here. How can I get file name from o ?

There is no way. Nothing says that 'ostream' has to be a file at all.
It can be a stringstream, for example.
}

void main() {

There is no 'void main' in C++.
bdd c(a,5,9) ;
ofstream out("junk") ;
out << c ;
}

V
 
A

Alf P. Steinbach

* (e-mail address removed):
ostream& operator<<(ostream& o, const bdd& d) {
I want to Print file name here. How can I get file name from o ?
}

As others have stated you cannot.

What's more, you cannot portably retrieve the underlying file handle,
even in the case where the ostream represents a file.

And even if you use some non-portable way to obtain the file handle
(assuming a file, and ostream functionality being implemented in terms
of something based on file handles), with a modern OS there may be no
way to obtain a unique filename, because a file may have more than one
name. E.g., that's the case in Unix-based OSes and in modern Windows.
However, it might be possible to obtain the name used to originally open
the file handle.

Upshot: if you need the/a file name, you need to let your own code
remember it.

void main() {

No such thing as 'void main' in C or C++, and never has been.
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top