🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #10395

Re: FileExists() is using wildcards

#10395
From: Ed Mulroy [TeamB]
Date: Tue, 27 Apr 2004 12:44
79 lines
3685 bytes
> > You might use one of these.
> >     return !access(name, 0);
>
> Interesting function. So DOS had it all already!

Yes, that is how I detected if a file existed when I switched to C
back in the DOS days.  Trying to open a file to detect if it exists is
slow and has other issues such as if the file were already open or
marked as hidden or system or read-only.  Reading the file attributes
is much faster and that is what access does.

> That did not compile for several reasons. The worst
> was that the compiler (bcb3) could not know about
> INVALID_FILE_ATTRIBUTES.
> ...
> You just typed that I suppose. What would be the
> right value for INVALID_FILE_ATTRIBUTES ?

I did not know that.

Yes, I just typed it in, but I use it a lot and how I typed it is how
I use it.

The Windows header files define the macro INVALID_FILE_ATTRIBUTES but
I just checked on my Win95 machine where BCB4 is loaded and as you
discovered that has not always been so.

When I looked at the help for GetFileAttributes using WIN32.HLP it
says the return value is 0xFFFFFFFF.  When I looked a the help
provided with the Platform SDK it says INVALID_FILE_ATTRIBUTES.  The
headers that came with BCB6 define INVALID_FILE_ATTRIBUTES to be
(DWORD(-1)) which is 0xFFFFFFFF.  I ran grep on the BCB4 header files
and they did not contain that macro.

I want to caution you against looking for FILE_ATTRIBUTE_NORMAL.  That
flag is a bit odd in its use and using it leaves you open to its vague
description.

.  Ed

> Hans Galema wrote in message
> news:408e0d39$2@newsgroups.borland.com...
>
> > You might use one of these.
> >     return !access(name, 0);
>
> Interesting function. So DOS had it all already!
>
> >   bool MyFileExists(const char *fname)
> >     {
> >     DWORD a = GetFileAttributes(name);
> >
> >     return (a != INVALID_FILE_ATTRIBUTES) &&
> >              !(a & FILE_ATTRIBUTE_DIRECTORY);
> >     }
>
> That did not compile for several reasons. The worst was that the
> compiler (bcb3) could not know about INVALID_FILE_ATTRIBUTES.
>
> I changed the code to:
>
>  bool MyFileExists2(const char *fname)
>    {
>    DWORD a = GetFileAttributes(fname);  // winbase.h
>
>    //return (a != INVALID_FILE_ATTRIBUTES) &&
>    //       !(a & FILE_ATTRIBUTE_DIRECTORY);
>
>    return (a != FILE_ATTRIBUTE_NORMAL) // 0x00000080
>mapiwin.h winnt.h
>               &&
>             !(a & FILE_ATTRIBUTE_DIRECTORY);// 0x00000010
> mapiwin.h winnt.h
>      }
>
> Now it does compile. And it works.
>
> You just typed that I suppose. What would be the right value
> for INVALID_FILE_ATTRIBUTES ?
Technical Details
Message-ID: <408e55bf$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <408d6cad$1@newsgroups.borland.com> <408d73fa$1@newsgroups.borland.com> <408e0d39$2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,8374fb71f8a662f4
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-04-27 06:10:35 PST
From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <408d6cad$1@newsgroups.borland.com> <408d73fa$1@newsgroups.borland.com> <408e0d39$2@newsgroups.borland.com>
Subject: Re: FileExists() is using wildcards
Date: Tue, 27 Apr 2004 08:44:36 -0400
Lines: 81
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 24.74.175.102
Message-ID: <408e55bf$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1083069887 24.74.175.102 (27 Apr 2004 05:44:47 -0700)
X-Authenticated-User: edmulroy
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail