|
Home > Archive > Unix Programming > May 2007 > file exists
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]
|
|
| Mr. X. 2007-05-29, 7:21 pm |
| Hello,
For C, on Lynox -
What is the command that check if the file exists (direct access) ?
Open return -1 if fails, and also lstat,
but I don't know if "fails" means the file doesn't exist.
Thanks 
| |
| Pascal Bourguignon 2007-05-29, 7:21 pm |
| "Mr. X." <no_spam_please@nospam_please.com> writes:
> For C, on Lynox -
I don't know Lynox. What kind of system is it?
> What is the command that check if the file exists (direct access) ?
> Open return -1 if fails, and also lstat,
> but I don't know if "fails" means the file doesn't exist.
What about typing at the prompt of the shell:
man 2 open
and reading the manual page of the open syscall?
When there is an error, it returns a negative integer, and the errno
variable is set to some error code listed in the manual page. Have a
look at ENOENT and ENOTDIR.
--
__Pascal Bourguignon__ http://www.informatimago.com/
NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
| |
| Mr. X. 2007-05-29, 7:21 pm |
| O.K.
If I want to create a file only if the file doesn't exist -
How can I do it on direct access functions (not buffered I/O),
and with minimal functions ?
Thanks 
"Pascal Bourguignon" <pjb@informatimago.com> wrote in message
news:87r6ozxtec.fsf@informatimago.com...
> "Mr. X." <no_spam_please@nospam_please.com> writes:
>
> I don't know Lynox. What kind of system is it?
>
>
> What about typing at the prompt of the shell:
>
> man 2 open
>
> and reading the manual page of the open syscall?
>
> When there is an error, it returns a negative integer, and the errno
> variable is set to some error code listed in the manual page. Have a
> look at ENOENT and ENOTDIR.
>
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> NOTE: The most fundamental particles in this product are held
> together by a "gluing" force about which little is currently known
> and whose adhesive power can therefore not be permanently
> guaranteed.
| |
| Gianni Mariani 2007-05-29, 7:21 pm |
| Pascal Bourguignon wrote:
> "Mr. X." <no_spam_please@nospam_please.com> writes:
>
> I don't know Lynox. What kind of system is it?
>
>
> What about typing at the prompt of the shell:
>
> man 2 open
>
> and reading the manual page of the open syscall?
>
> When there is an error, it returns a negative integer, and the errno
> variable is set to some error code listed in the manual page. Have a
> look at ENOENT and ENOTDIR.
you can also try access - again :
man 2 access
If this program you're writing is likely to have more than one instance
run at the same time, then you really want to think hard about possible
race conditions.
If all you're doing is wanting to know if a file exists, then access I
believe does not modify the the modification or access times of a file.
Be careful when using it in a SUID program as it behaves a little
differently.
| |
| Gianni Mariani 2007-05-29, 7:21 pm |
| Mr. X. wrote:
> O.K.
> If I want to create a file only if the file doesn't exist -
> How can I do it on direct access functions (not buffered I/O),
> and with minimal functions ?
It's in the man page. O_EXCL | O_CREAT
| |
| Gianni Mariani 2007-05-29, 7:21 pm |
| Mr. X. wrote:
> O.K.
> If I want to create a file only if the file doesn't exist -
> How can I do it on direct access functions (not buffered I/O),
> and with minimal functions ?
It's in the man page. O_EXCL | O_CREAT
| |
| Mr. X. 2007-05-29, 7:21 pm |
| O.K - found.
"Gianni Mariani" <gi3nospam@mariani.ws> wrote in message
news:465c9a09$0$17985$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
> Mr. X. wrote:
>
> It's in the man page. O_EXCL | O_CREAT
| |
| Gordon Burditt 2007-05-30, 1:21 am |
| >For C, on Lynox -
Lynox?
>What is the command that check if the file exists (direct access) ?
There are various possibilities. stat(), lstat(), open(), access(),
and unlink() come to mind. unlink() is kinda brutal: if it returns
successfully, the file used to exist.
>Open return -1 if fails, and also lstat,
>but I don't know if "fails" means the file doesn't exist.
It is perfectly legitimate for the OS to return a value which means
"You do not have sufficient permission to know whether it exists
or not". Values of errno such as ENOENT or ENOTDIR suggest that
nothing by that name exists (but you might not be able to create
it either, at least not without creating intermediate directories
first). Values such as EACCES suggest that you're not allowed to
know.
access() might not work like you want it to if it is used in or
invoked from a setuid program.
Remember that if you intend to check if the file exists and then
create it if it doesn't, the best you can do is check if the file
exists and then A WHOLE LOT LATER (maybe even an entire machine
instruction) try to create it if it hasn't already been created in
the meantime. Watch for race conditions. open() with the O_EXCL
option might be useful here.
| |
|
| On May 30, 1:10 am, "Mr. X." <no_spam_please@nospam_please.com> wrote:
> Hello,
>
> For C, on Lynox -
> What is the command that check if the file exists (direct access) ?
> Open return -1 if fails, and also lstat,
> but I don't know if "fails" means the file doesn't exist.
>
> Thanks 
What is this lynox..... I suppose it is Linux
you can however check the errno to confirm whether the file exist or
not..
Any way try the access() function. It takes file name and test_type
as argument.
int access (char *file_name, int flag); // return 0 on success and
-1 on error
where flag is symbolic constant defined in fcntl.h or unistd.h (not
sure...may be)
F_OK - file exist
R_OK - readable
W_OK - writable
X_OK - executable
Bye
Guru Jois
|
|
|
|
|