file exists
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > file exists




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    file exists  
Mr. X.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 12:21 AM

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 







[ Post a follow-up to this message ]



    Re: file exists  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 12:21 AM

"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.





[ Post a follow-up to this message ]



    Re: file exists  
Mr. X.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 12:21 AM

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.







[ Post a follow-up to this message ]



    Re: file exists  
Gianni Mariani


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 12:21 AM

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.





[ Post a follow-up to this message ]



    Re: file exists  
Gianni Mariani


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 12:21 AM

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





[ Post a follow-up to this message ]



    Re: file exists  
Gianni Mariani


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 12:21 AM

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





[ Post a follow-up to this message ]



    Re: file exists  
Mr. X.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 12:21 AM

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







[ Post a follow-up to this message ]



    Re: file exists  
Gordon Burditt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 06: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.






[ Post a follow-up to this message ]



    Re: file exists  
guru


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-07 12:17 PM

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






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 07:43 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register