Re: File exist
- From: Kenneth Brody <kenbrody@xxxxxxxxxxx>
- Date: Wed, 19 Apr 2006 10:25:48 -0400
Flash Gordon wrote:
Kenneth Brody wrote:
Keith Thompson wrote:
[...]
Usually the best approach is to try to open the file, and handle the
error if the attempt fails. Providing information about *why* it
failed can be useful, but it isn't absolutely necessary, and it can't
be done portably.
Isn't this portable?
... proper #include's, etc. implied ...
errno=0;
f = fopen(filename,mode);
if ( f == NULL )
{
if ( errno != 0 )
perror(filename);
else
fprintf(stderr,"fopen() of %s failed.\n",filename);
exit(EXIT_FAILURE);
}
It's portable, but it might always print out "fopen() of %s failed.\n"
and never give a reason on some implementations.
Which I suppose qualifies for "can't be done portably" in terms of
actually getting the reason. However, the above is portable, and
will give the reason on those platforms that tell you why. On
platforms that don't tell you why, there's not much that you can
do, is there? (Even non-portably. Unless the "real" error number
is stored somewhere, but not placed in errno, sort of like the
Windows GetLastError() call. Of course, all the Windows C compilers
that I've seen set errno for you.)
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@xxxxxxxxx>
.
- References:
- File exist
- From: paytam
- Re: File exist
- From: ed
- Re: File exist
- From: Keith Thompson
- Re: File exist
- From: Claude Yih
- Re: File exist
- From: Flash Gordon
- Re: File exist
- From: Mr John FO Evans
- Re: File exist
- From: Keith Thompson
- Re: File exist
- From: Kenneth Brody
- Re: File exist
- From: Flash Gordon
- File exist
- Prev by Date: Re: How can I pass command line arguments in Visual Studio 2005 (in C++ environment)
- Next by Date: Re: Equality of floating-point numbers (special case)
- Previous by thread: Re: File exist
- Next by thread: Re: File exist
- Index(es):
Relevant Pages
|