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

fail to open the file

P: n/a

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

Oct 16 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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.

Oct 16 '05 #2

P: n/a

gu*****@gmail.com wrote:
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

Oct 16 '05 #3

P: n/a
>
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)
Oct 16 '05 #4

P: n/a

Sebastian Wiesner wrote:

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)

Not really sure what you mean. Be clear.

Oct 16 '05 #5

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

Sebastian Wiesner wrote:
>
> 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)

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)

Oct 16 '05 #6

P: n/a

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

Sebastian Wiesner wrote:
>
> 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)

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)

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

Oct 16 '05 #7

P: n/a
Once upon a time (Sonntag, 16. Oktober 2005 18:15) puzzlecracker wrote some
very nice things:

Sebastian Wiesner wrote:
Once upon a time (Sonntag, 16. Oktober 2005 17:52) puzzlecracker wrote
some very nice things:
>
> Sebastian Wiesner wrote:
>> >
>> > 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)
>
>
> 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)

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.
Oct 16 '05 #8

P: n/a
puzzlecracker wrote:
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()
Oct 17 '05 #9

P: n/a
Sebastian Wiesner wrote:
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)

Were you eating toast?
Oct 17 '05 #10

P: n/a
Once upon a time (Dienstag, 18. Oktober 2005 01:25) Ron Natalie wrote some very
nice things:
Sebastian Wiesner wrote:
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)

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
Oct 18 '05 #11

P: n/a

Sebastian Wiesner wrote:
Once upon a time (Dienstag, 18. Oktober 2005 01:25) Ron Natalie wrote some very
nice things:
Sebastian Wiesner wrote:
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)

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

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

Oct 22 '05 #12

P: n/a
puzzlecracker wrote:
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

Oct 22 '05 #13

P: n/a
puzzlecracker wrote:


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;
}
Oct 22 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.