Re: How to check if file exists ?
- From: "Hector Santos" <nospamhere@xxxxxxxxxxxxxx>
- Date: Sun, 6 Aug 2006 05:57:24 -0400
"Eugene Gershnik" <gershnik@xxxxxxxxxxx> wrote in message
news:#o8uhxLuGHA.476@xxxxxxxxxxxxxxxxxxxxxxx
if (FileExist(szFileSemaphore)) {
DeleteFile(szFileSemaphore);
DoSomeThing()
}
This is simply incorrect and inefficient. Directly check that
the deletion succeeded in the if.
I not am sure why you say this. First it is example, and second, it is
perfectly fine.
Another application for a "file semaphore" is as a "Lock" concept.
Which doesn't really work with existence check only.
But thousands of applications of the past 30+ years used this logic. How
could it now work?
There were really all kinds of reasons for them.
Which all probably require opening a file and having a
lock on it (that's why file locking was added to
Unix long ago). "Existence" check alone is not
enough to ensure correctness for any lock or semaphore scenario.
I disagree. There is no need to open it. What's wrong using
GetFileAttribute() with a check with errors?
There are precious few situations where you can have system
which is *correct* and doesn't require opening a file.
But I just told you that thousands of applications for hetergenoous
application integration, poor may sychronziation between remote processes,
and its been doing this for a very long time, most of which help define the
mail market you are using today. You don't need to open the file.
One legitimate scenario I know
about is in Word's Save As algorithm for unnamed files. When Untitled
already exists in the destination folder it suggests Untitled-1 or some
such. Of course it has to check again when it actually saves.
And how will it do it? By opening it? Or not Opening it? How do you know
how it is being done? How do you know its not using a
FindFirstFile/FindNext() to find the highest number? I don't see this as
quite the same thing.
The key thing here is that there many reasons file semaphores are used and
you don't need to open it. This includes applicaitons that might not even
care if a file is locked or not "viewable" but the file system. The
application simply wants to now if the file existence.
What you are suggesting is that one can not reliable use:
BOOL FileExist(const char *sz)
{
return GetFileAttributes(sz) != 0xFFFFFFFF;
}
and his application will never work?
How do you know that his application is guaranteed to expect the file to be
never hidden. So what if it errors out. As far the application is concern,
its not ready or exist for whatever reason.
Don't get me wrong, I understand completely what you are saying. The file
may exist and you get a false negative. But maybe the application doesn't
care because the application is 100% confident that at some future moment it
will be available.
The fact is thousands of applications over decades have use this fine
without a problem.
Also consider batch files
:top
if not exist MAIL.BUSY goto :top
.... process whatever ....
This is what thousands of people do across many application scenarios. They
don't care if for some moment the result was false because it was in some
"intermediate state."
Understand?
--
Hector Santos, Santronics Software, Inc.
http://www.santronics.com
.
- Follow-Ups:
- Re: How to check if file exists ?
- From: Eugene Gershnik
- Re: How to check if file exists ?
- From: Alexander Grigoriev
- Re: How to check if file exists ?
- From: Carl Daniel [VC++ MVP]
- Re: How to check if file exists ?
- References:
- How to check if file exists ?
- From: Vilius
- Re: How to check if file exists ?
- From: Eugene Gershnik
- Re: How to check if file exists ?
- From: Hector Santos
- Re: How to check if file exists ?
- From: Eugene Gershnik
- How to check if file exists ?
- Prev by Date: Re: pausing system clock
- Next by Date: Re: How to check if file exists ?
- Previous by thread: Re: How to check if file exists ?
- Next by thread: Re: How to check if file exists ?
- Index(es):
Relevant Pages
|