Checking if a file exists

A

Army1987

How are you going to do I/O /without/ it, in a portable manner?

<ot>
Not portable everywhere, but
#include <stdlib.h>
#include <string.h>
#define MAX 4090
int main(void)
{
char cmd[MAX+6] = "echo ";
system(strncat(cmd, "Hello, World!", MAX));
return 0;
}
should work on several systems. Input is trickier... :) </ot>
 
R

Richard Bos

Eric Sosman said:
Keith said:
Army1987 said:
Is this a good way to check wheter a file already exists?
[snip]

Testing whether a file exists often (not always, but often) means
you're asking the wrong question.

Why do you want to know whether the file exists? If it's so you can
decide whether to attempt some operation (e.g., reading from the file
if it does exist, or creating it if it doesn't), it's often better to
just go ahead and attempt the operation, and handle the error if it
fails. (This can be difficult in standard C if you're trying to
create a file; if I recall correctly, it's implementation-defined
whether attempting to create an existing file will clobber the file or
fail, but there are often system-specific ways to control this
behavior.)

I would prefer to see "File IRREPLACABLE.DAT exists.
Overwrite?" than to regret it at leisure ...

So would I, but there are circumstances where that is just not possible.

This is what you allude to in "system-specific ways," but
unfortunately Standard C's I/O is too diluted to do the job
unaided.

YM too portable. I don't think there _is_ a way to test for file
existence that would work everywhere <stdio.h> does.

Richard
 
G

Guest

Richard said:
YM too portable. I don't think there _is_ a way to test for file
existence that would work everywhere <stdio.h> does.

If you mean exactly what you said, that doesn't seem relevant to me.
There's no way to open a file that works everywhere except for
<stdio.h>'s fopen, that's why fopen's there. Similarly, if there's no
way to test for a file's existence that works everywhere, that could
be why <stdio.h> should have provided a function for that.

If you mean that some systems supporting <stdio.h> functionality don't
provide any way at all to test whether a file exists, could you give
an example? It doesn't have to be a real world example, just a reason
of why a hypothetical implementation might not be able to provide it.
I can understand that media may be damaged, or directories may have
restricted access, so I can understand why a general fexist() that
returns true or false might be unimplementable, but in the specific
context, might any system not be able to reliably answer "if I try to
create file XXX, would I get an error, would I get a new file, or
would I overwrite an existing file"?
 
G

Guest

Richard said:
YM too portable. I don't think there _is_ a way to test for file
existence that would work everywhere <stdio.h> does.

If you mean exactly what you said, that doesn't seem relevant to me.
There's no way to open a file that works everywhere except for
<stdio.h>'s fopen, that's why fopen's there. Similarly, if there's no
way to test for a file's existence that works everywhere, that could
be why <stdio.h> should have provided a function for that.

If you mean that some systems supporting <stdio.h> functionality don't
provide any way at all to test whether a file exists, could you give
an example? It doesn't have to be a real world example, just a reason
of why a hypothetical implementation might not be able to provide it.
I can understand that media may be damaged, or directories may have
restricted access, so I can understand why a general fexist() that
returns true or false might be unimplementable, but in the specific
context, might any system not be able to reliably answer "if I try to
create file XXX, would I get an error, would I get a new file, or
would I overwrite an existing file"?
 
K

Kenneth Brody

Harald van D?k wrote:
[...]
If you mean that some systems supporting <stdio.h> functionality don't
provide any way at all to test whether a file exists, could you give
an example? It doesn't have to be a real world example, just a reason
of why a hypothetical implementation might not be able to provide it.
[...]

I seem to recall that someone posted several months back about a
"cryptographic filesystem", for which there was no way to test a
file's existence.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
G

Guest

Walter said:

Thank you and Kenneth for the information. As I understand it from
these messages, on such a filesystem, reading foo.txt would always
succeed, regardless of whether it had ever been written to, and
writing to it would destroy any information stored in a file using any
other filename. Doesn't that mean a file's existence can reliably be
determined? It simply always exists.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top