Re: File exist



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>


.



Relevant Pages

  • Re: Future of MFC?
    ... reason to not use .Net. ... for native code development, and with tools that were as good as those ... If those things matter to you you won't be interested in MFC, ... pretty-much a rarity on non-Windows platforms (and can't be guaranteed ...
    (microsoft.public.vc.mfc)
  • Re: Why are z/OS people reluctant to use z/OS UNIX?
    ... offend anyone's sensibilities (I'm an avid mainframe guy, ... The thing is, most new applications, features, platforms, et al, are fraught ... One reason that Linux exploded onto the scene is that it has a user ... I firmly believe that if IBM would ...
    (bit.listserv.ibm-main)
  • Re: SharedCLibrary version
    ... The guard could be there for a very good reason. ... any RMEnsures. ... If however a programmer, for whatever reason, decides not to use ... platforms he wants to support. ...
    (comp.sys.acorn.programmer)
  • Re: Fast / Slow Line Layout
    ... That it has happened over time is part of the reason, ... at flat junctions a train may have to cross all other lines to make ... more easily by just putting the platforms on the outside. ... Interchange between trains in the same direction can easily be ...
    (uk.railway)
  • Re: [PATCH 1/2] OLPC: Add support for calling into Open Firmware
    ... tree, etc. ... There's potentially no reason why other platforms couldn't use this, ... currently OLPC is the main user of it. ...
    (Linux-Kernel)