|
Home > Archive > Unix Programming > June 2005 > How to check whether file exists or not
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to check whether file exists or not
|
|
| niraj.kumar.ait@gmail.com 2005-06-29, 2:48 am |
| How to check whether a file exists or not at its speciefied location
..Is there any system call for this
TIA
Niraj
| |
| Bill Marcum 2005-06-29, 2:48 am |
| On 28 Jun 2005 22:46:23 -0700, niraj.kumar.ait@gmail.com
<niraj.kumar.ait@gmail.com> wrote:
> How to check whether a file exists or not at its speciefied location
> .Is there any system call for this
>
stat
--
Tonight you will pay the wages of sin; Don't forget to leave a tip.
| |
| SM Ryan 2005-06-29, 2:48 am |
| niraj.kumar.ait@gmail.com wrote:
# How to check whether a file exists or not at its speciefied location
# .Is there any system call for this
Within a C program you can use access or stat
man 2 access
man 2 stat
In a shell script, you can use variants of test (or [...])
$ touch Q
$ if [ -e Q ]; then echo est; else echo non; fi
est
$ if [ -e QX ]; then echo est; else echo non; fi
non
man 1 test
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.
| |
| William Ahern 2005-06-29, 7:53 am |
| niraj.kumar.ait@gmail.com wrote:
> How to check whether a file exists or not at its speciefied location
> .Is there any system call for this
There's stat(2), as other's have mentioned. Also access(2). You can also
just try to open the file using fopen(3) or open(2). In fact, if your
intention is to open the file if it exists than simply open it, otherwise
you have a race condition and/or are doing duplicate work.
- Bill
| |
| Rich Teer 2005-06-29, 5:53 pm |
| On Tue, 28 Jun 2005 niraj.kumar.ait@gmail.com wrote:
> How to check whether a file exists or not at its speciefied location
> .Is there any system call for this
You can use access(), stat(), or open() to check for a file's existance,
but if you plan to check for a file existance before opening, beware that
this introduces a race condition to your program. In this instance, it's
better to just open() the file and deal with any errors.
HTH,
--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
|
|
|
|
|