如何检查文件存在。 [英] How to check file exists.

查看:157
本文介绍了如何检查文件存在。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


要使用文件名查找存在的文件,我在UNIX上有两个路线

系统。


1.访问(2)

2. lstat(2)


这个拖曳功能也可以。当返回值为-1时,返回值为-1。和errno

是ENOENT,文件不存在。


**哪一个更好?

非常感谢。

Raymond Shen。

Hi All:

To find a file exists using the file name, I have tow routings on UNIX
system.

1. access(2)
2. lstat(2)

This tow function also can do. When the return value is "-1" and errno
is "ENOENT", The file doesn''t exist.

**Which one is better?

Thank you very much.
Raymond Shen.

推荐答案

2005年5月27日星期五11: 35:43 +0800,Raymond< rs ******* @ yahoo.com>

在comp.lang.c中写道:
On Fri, 27 May 2005 11:35:43 +0800, Raymond <rs*******@yahoo.com>
wrote in comp.lang.c:
大家好:

要使用文件名查找存在的文件,我在UNIX
系统上有两个路由。

1. access(2)
2. lstat(2)
这个拖曳功能也可以做到。当返回值为-1时,返回值为-1。和errno
是ENOENT,文件不存在。

**哪一个更好?

非常感谢。
Raymond沉。
Hi All:

To find a file exists using the file name, I have tow routings on UNIX
system.

1. access(2)
2. lstat(2)

This tow function also can do. When the return value is "-1" and errno
is "ENOENT", The file doesn''t exist.

**Which one is better?

Thank you very much.
Raymond Shen.




两者都没有,因为它们都不是标准C的一部分,所以它们在这里是非主题的。如果你想比较UNIX提供的非标准扩展

,请在新闻中询问:comp.unix.programmer。


-

Jack Klein

主页: http:// JK-Technology。 Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang .c ++ http://www.parashift.com/c++-faq-lite /

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



Neither, because neither of them is part of standard C, so they are
off-topic here. If you want a comparison of non-standard extensions
provided by UNIX, ask in news:comp.unix.programmer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


>要使用文件名查找存在的文件,我在UNIX
>To find a file exists using the file name, I have tow routings on UNIX
系统上有两个路由。

1。访问(2)
2。 lstat(2)
这个拖曳功能也可以做到。当返回值为-1时,返回值为-1。和errno
是ENOENT,文件不存在。

**哪一个更好?
system.

1. access(2)
2. lstat(2)

This tow function also can do. When the return value is "-1" and errno
is "ENOENT", The file doesn''t exist.

**Which one is better?




fopen()具有标准C中存在的优点,如果

成功,则文件存在并且您可以访问它。


所有3个函数有不同的方式,他们可以给出一个优秀的答案:


- lstat()可以给出误报,取决于你的想法

的情况,如果名称是指一个破损的符号链接。

- access()可能会因权限而给出误报或否定

问题如果有效和真实的用户ID是不同的。

- 所有3个都可以通过errno == EPERM失败,你不知道

文件是否存在,因为你没有权限知道这个。

- fopen()对存在的文件失败但是你没有权限

来使用这个模式(读取或写你要求的。


Gordon L. Burditt



fopen() has the advantage of existing in standard C, and if
it succeeds, the file exists and you can access it.

All 3 functions have different ways in which they can give an
indecisive answer:

- lstat() can give a false positive, depending on what you think about
the situation, if the name refers to a broken symlink.
- access() can give a false positive or negative due to permissions
issues if the effective and real user ids are different.
- All 3 can fail with errno == EPERM and you don''t know whether the
file exists or not, because you don''t have permission to know that.
- fopen() fails on files which exist but you don''t have permission
to use the mode (read or write) which you asked for.

Gordon L. Burditt


Gordon Burditt写道:
Gordon Burditt wrote:
要使用文件名查找存在的文件,我在UNIX
系统上有两个路由。

1。访问(2)
2。 lstat(2)
这个拖曳功能也可以做到。当返回值为-1时,返回值为-1。和errno
是ENOENT,文件不存在。

**哪个更好?

fopen()的优点是存在于标准C中,如果成功,则文件存在并且您可以访问它。
To find a file exists using the file name, I have tow routings on UNIX
system.

1. access(2)
2. lstat(2)

This tow function also can do. When the return value is "-1" and errno
is "ENOENT", The file doesn''t exist.

**Which one is better?

fopen() has the advantage of existing in standard C, and if
it succeeds, the file exists and you can access it.




谢谢Gordon。

但我只想知道文件是否存在。如果fopen成功,则打开

文件并分配系统文件处理程序。我必须关闭

处理程序。考虑应用程序的性能。我想使用

打火机布线。

所有3种功能都有不同的方式可以给出一个优柔寡断的答案:

- lstat()可以给出误报,取决于你对这种情况的看法,如果名称是指一个破坏的符号链接。
- access()可以给出误报或否定由于权限问题,如果有效和真实的用户ID不同。
- 所有3个都可以通过errno == EPERM失败,你不知道
文件是否存在,因为你没有权限知道这一点。
- fopen()对存在的文件失败但你没有权限使用你要求的模式(读或写) 。

Gordon L. Burditt



Thank you Gordon.
But I only want to know whether the file exists. If fopen success, The
file is open and a system file handler is assigned. I must close the
handler. Consider the performance of the application. I''d like use a
lighter routing.

All 3 functions have different ways in which they can give an
indecisive answer:

- lstat() can give a false positive, depending on what you think about
the situation, if the name refers to a broken symlink.
- access() can give a false positive or negative due to permissions
issues if the effective and real user ids are different.
- All 3 can fail with errno == EPERM and you don''t know whether the
file exists or not, because you don''t have permission to know that.
- fopen() fails on files which exist but you don''t have permission
to use the mode (read or write) which you asked for.

Gordon L. Burditt



这篇关于如何检查文件存在。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆