Does ifstream know why it is unable to open file?

A

Alex Vinokur

-------------------------------
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:[email protected]
http://mathforum.org/library/view/10978.html
=====================================
 
F

Frank Schmitt

Alex Vinokur said:
-------------------------------
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
 
K

Kevin Goodsell

Frank said:
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
 

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,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top