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

Does ifstream know why it is unable to open file?

P: n/a
-------------------------------
ifstream fin ("foo.txt");
if (!fin) cout << "Warning: Unable to open file" << endl;
-------------------------------

There are several reasons for getting that warning :
* foo.txt doesn't exist
* foo.txt exists but is busy
* etc.

Can ifstream know why it is unable to open the file?

=====================================
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html
=====================================
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Alex Vinokur" <al****@bigfoot.com> writes:
-------------------------------
ifstream fin ("foo.txt");
if (!fin) cout << "Warning: Unable to open file" << endl;
-------------------------------

There are several reasons for getting that warning :
* foo.txt doesn't exist
* foo.txt exists but is busy
* etc.

Can ifstream know why it is unable to open the file?


If it knows, it's platform-dependent (as Victor already
pointed out).
If you don't care about portability, You might want to check
if your implementation sets errno upon failure - some of them do.

HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frank DOT schmitt AT 4sc DOT com
Jul 19 '05 #2

P: n/a
Frank Schmitt wrote:
"Alex Vinokur" <al****@bigfoot.com> writes:
Can ifstream know why it is unable to open the file?

If it knows, it's platform-dependent (as Victor already
pointed out).
If you don't care about portability, You might want to check
if your implementation sets errno upon failure - some of them do.


Why would checking errno require not caring about portability? errno is
standard. Either the function will set it or not. It's easy to check and
use it if it has a meaningful value.

But I don't know how common it is for implementations to set errno in
this case.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.