determining failure cause of an fstream call

R

Rob Mandeville

I need to open a file, and determine if:

1: I actually opened it
2: I couldn't open it because it doesn't exist, or
3: I couldn't open it for other reasons, such as file permissions.

I'm using ifstream for this, and I can tell if it opened or not. But if
it doesn't open, how do I tell whether the file exists or not?

Here's what I have so far:

code:

ifstream foo();
foo.open(filename);
if(foo.is_open()){
// It's open...read it.
}else if(BAR){ // file does not exist
// handle file does not exist case
}else{
// complain to user that you can't open the file
}

So I need to know what I can put in place of BAR.

Can I trust the value of errno in this case? Is BAR "(errno==ENOENT)"?

I need the same code working in VC++ and unix/g++, so I'm looking for a
platform-agnostic solution, if one exists.

--Rob
 
J

James Aguilar

Rob Mandeville said:
I need to open a file, and determine if:

1: I actually opened it
2: I couldn't open it because it doesn't exist, or
3: I couldn't open it for other reasons, such as file permissions.

I'm using ifstream for this, and I can tell if it opened or not. But if
it doesn't open, how do I tell whether the file exists or not?

To my knowledge, standard C++ doesn't know about permissions. I am a total
newbie, though, so I could be wrong. Source:

http://www.cplusplus.com/ref/iostream/ios/exceptions.html

It doesn't seem to have anything except fail, which kind of makes sense,
considering permissions are not necessarily available on all platforms.

- James Aguilar
 
C

Chris \( Val \)

|I need to open a file, and determine if:
|
| 1: I actually opened it
| 2: I couldn't open it because it doesn't exist, or
| 3: I couldn't open it for other reasons, such as file permissions.
|
| I'm using ifstream for this, and I can tell if it opened or not. But if
| it doesn't open, how do I tell whether the file exists or not?
|
| Here's what I have so far:
|
| code:
|
| ifstream foo();
| foo.open(filename);
| if(foo.is_open()){
| // It's open...read it.
| }else if(BAR){ // file does not exist
| // handle file does not exist case
| }else{
| // complain to user that you can't open the file
| }

There is no portable way in *Standard C++* to know
for sure if a file exists or not - You will have to
resort to non-standard features for that.

Additionally, C++ has no notion of permissions, and
again you will need to look into the extensions
provided by your platform.

| So I need to know what I can put in place of BAR.

About the best you can do is:

else if( !foo )
// ...

It will indicate a failure to open the stream, but
it does not mean that the file does not exist.

| Can I trust the value of errno in this case? Is BAR "(errno==ENOENT)"?

If the documentation for your implementation says
so, then why don't you just try it ?

| I need the same code working in VC++ and unix/g++, so I'm looking for a
| platform-agnostic solution, if one exists.

I doubt you will get exactly what you need.

At best, you might want to use conditional compilation
with the pre-processor for implementation specifics.

Cheers.
Chris Val
 

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

fstream Buffers 26
fstream File i/o 1
Test if a std::fstream is open 1
eof error using '>>' in fstream 2
fstream.. 3
problem with fstream 3
fstream vs ofstream 6
fstream :: more information about failure 4

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top