fail to open the file

P

puzzlecracker

I would like to find out why exactly I wasn't able to open or execute
the file.... how is it done in c++?


ifsteam in("somefile");

if(!in)

//why?


Thanks
 
G

guyarad

that code should work.
but you should pay attention to some points:
1. make sure the file exists, and is in the current directory of your
program.
if you are using a debugger it might be a working dir of the project.
2. make sure the file is not in use by another application.

if you checked this, try to open the file using ios::in and
ios::nocreate flags and after the call check ios::failbit flag. if it
is set, one of 3 things have happened: the file is not found, the
filebuf object already attached to an open file or a filebuf call
fails.
anyway, if you are using windows, try the win32api CreateFile or
OpenFile to get an extended error report.

for future errors, try look at the documentation of the function to
find out how errors are reported the the reasons for the errors.
 
P

puzzlecracker

that code should work.
but you should pay attention to some points:
1. make sure the file exists, and is in the current directory of your
program.
if you are using a debugger it might be a working dir of the project.
2. make sure the file is not in use by another application.

if you checked this, try to open the file using ios::in and
ios::nocreate flags and after the call check ios::failbit flag. if it
is set, one of 3 things have happened: the file is not found, the
filebuf object already attached to an open file or a filebuf call
fails.
anyway, if you are using windows, try the win32api CreateFile or
OpenFile to get an extended error report.

for future errors, try look at the documentation of the function to
find out how errors are reported the the reasons for the errors.


I am working under unix and need to check for two failures (need to
know exactly which one took place): permission denied or file doesn't
exist.

How would do that?


I'm pretty sure it is covered in the std.


Thanks
 
S

Sebastian Wiesner

I am working under unix and need to check for two failures (need to
know exactly which one took place): permission denied or file doesn't
exist.
1. Checking the permissions berfore trying to open the file with access
(type man 2 access on your shell for more information)
 
P

puzzlecracker

Sebastian said:
1. Checking the permissions berfore trying to open the file with access
(type man 2 access on your shell for more information)


Not really sure what you mean. Be clear.
 
S

Sebastian Wiesner

Once upon a time (Sonntag, 16. Oktober 2005 17:52) puzzlecracker wrote some
very nice things:
Not really sure what you mean. Be clear.

sry.
You can use the system function "access" to check the permission of a file.
The documentation for this function is given on its man page. To see that
page, type "man 2 access" on your shell. (the 2 is needed to get to the
function's man page, because there is a tool thats also named access and,
guess, it has the the same purpose)
 
P

puzzlecracker

Sebastian said:
Once upon a time (Sonntag, 16. Oktober 2005 17:52) puzzlecracker wrote some
very nice things:


sry.
You can use the system function "access" to check the permission of a file.
The documentation for this function is given on its man page. To see that
page, type "man 2 access" on your shell. (the 2 is needed to get to the
function's man page, because there is a tool thats also named access and,
guess, it has the the same purpose)


That C function I know - I am more interested in portable C++ way to
accomplish that.
 
S

Sebastian Wiesner

Once upon a time (Sonntag, 16. Oktober 2005 18:15) puzzlecracker wrote some
very nice things:
That C function I know - I am more interested in portable C++ way to
accomplish that.

I don't think that there is any _portable_ way to do that, since each OS has
its own security model, which may or may not be compatible to each other.
You will have to write different code for each platform you want to
support.

If you don't like C, none can help you. Unix is implemented in C and not in
C++, so if you want to access system specific features like permissions,
you will have to deal with C.
 
R

red floyd

puzzlecracker said:
I would like to find out why exactly I wasn't able to open or execute
the file.... how is it done in c++?


ifsteam in("somefile");

if(!in)

//why?


Thanks

I believe errno and its friends strerror() and perror() are in the Standard.

#include <cerrno> // for errno
#include <cstring> // for strerror()
#include <cstdio> // for perror()
 
R

Ron Natalie

Sebastian said:
1. Checking the permissions berfore trying to open the file with access
(type man 2 access on your shell for more information)
Were you eating toast?
 
S

Sebastian Wiesner

Once upon a time (Dienstag, 18. Oktober 2005 01:25) Ron Natalie wrote some very
nice things:
Were you eating toast?

Englisch isnt my native language.
So, Sorry, but i don't no what youre trying to say.
i can understand the meaning, but it makes absolutly no sense to me.

Basti
 
P

puzzlecracker

Sebastian said:
Once upon a time (Dienstag, 18. Oktober 2005 01:25) Ron Natalie wrote some very
nice things:


Englisch isnt my native language.
So, Sorry, but i don't no what youre trying to say.
i can understand the meaning, but it makes absolutly no sense to me.

Basti


he says that it is overhead to use access() (also implying that your
c++ knowledge needs some polishing) to find permissions... perror does
just that in portable way. Additionally, what we're really looking for
is c++ equivalent for error
 
J

Jonathan Mcdougall

puzzlecracker said:
I would like to find out why exactly I wasn't able to open or execute
the file.... how is it done in c++?


ifsteam in("somefile");

if(!in)

//why?

It is impossible in standard C++ to know *why* the stream could not be
opened. You'll have to be platform-specific.


Jonathan
 
R

red floyd

puzzlecracker said:
he says that it is overhead to use access() (also implying that your
c++ knowledge needs some polishing) to find permissions... perror does
just that in portable way. Additionally, what we're really looking for
is c++ equivalent for error

Again, I would point out that errno, perror, and strerror are all part
of Standard C++ (see <cerrno>, <cstdio>, and <cstring>). There's
absolutely nothing keeping you from using them.

e.g.

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>

int main()
{
std::ifstream ifs("foo.txt");
if (!ifs)
std::cerr << "couldn't open foo.txt: "
<< strerror(errno)
<< std::endl;
return 0;
}
 

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
474,147
Messages
2,570,833
Members
47,378
Latest member
BlakeLig

Latest Threads

Top