By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

How can I get file name from ostream ?

P: n/a

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

Feb 24 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sa***********@yahoo.com wrote:
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
Feb 25 '07 #2

P: n/a
sa***********@yahoo.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 25 '07 #3

P: n/a
* sa***********@yahoo.com:
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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.