C++ 检查 Unix 中是否存在目录(系统调用)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3828192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a directory exists in Unix (system call)
提问by Jary
I am not able to find a solution to my problem online.
我无法在网上找到我的问题的解决方案。
I would like to call a function in Unix, pass in the path of a directory, and know if it exists. opendir()
returns an error if a directory does not exist, but my goal is not to actually open, check the error, close it if no error, but rather just check if a file is a directory or not.
我想在Unix中调用一个函数,传入一个目录的路径,并知道它是否存在。opendir()
如果目录不存在,则返回错误,但我的目标不是实际打开,检查错误,如果没有错误则关闭它,而只是检查文件是否是目录。
Is there any convenient way to do that please?
请问有什么方便的方法吗?
回答by Jonathan Leffler
There are two relevant functions on POSIX systems: stat() and lstat(). These are used to find out whether a pathname refers to an actual object that you have permission to access, and if so, the data returned tells you what type of object it is. The difference between stat()
and lstat()
is that if the name you give is a symbolic link, stat()
follows the symbolic link (or links if they are chained together) and reports on the object at the end of the chain of links, whereas lstat()
reports on the symbolic link itself.
POSIX 系统上有两个相关函数:stat() 和 lstat()。这些用于确定路径名是否引用了您有权访问的实际对象,如果是,则返回的数据会告诉您它是什么类型的对象。stat()
和之间的区别在于lstat()
,如果您给出的名称是符号链接,则stat()
跟随符号链接(或链接,如果它们链接在一起)并报告链接链末端的对象,而lstat()
报告符号链接本身.
#include <sys/stat.h>
struct stat sb;
if (stat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode))
{
...it is a directory...
}
If the function indicates it was successful, you use the S_ISDIR() macro from <sys/stat.h>
to determine whether the file is actually a directory.
如果该函数指示它成功,则使用 S_ISDIR() 宏 from<sys/stat.h>
来确定该文件是否实际上是一个目录。
You can also check for other file types using other S_IS*
macros:
您还可以使用其他S_IS*
宏检查其他文件类型:
S_ISDIR
— directoryS_ISREG
— regular fileS_ISCHR
— character deviceS_ISBLK
— block deviceS_ISFIFO
— FIFOS_ISLNK
— symbolic linkS_ISSOCK
— socket
S_ISDIR
- 目录S_ISREG
— 常规文件S_ISCHR
— 字符设备S_ISBLK
— 块设备S_ISFIFO
— 先进先出S_ISLNK
— 符号链接S_ISSOCK
- 插座
(Some systems provide a few other file types too; S_ISDOOR
is available on Solaris, for example.)
(某些系统也提供了一些其他文件类型;S_ISDOOR
例如,可在 Solaris 上使用。)
回答by codaddict
You can make use of the stat
system call by passing it the name of the directory as the first argument. If the directory exists a 0
is returned else -1
is returned and errno will be set to ENOENT
您可以stat
通过将目录名称作为第一个参数传递给系统调用来使用系统调用。如果目录存在0
则返回,否则-1
返回 errno 将设置为ENOENT
EDIT:
编辑:
If the return value is 0
you would need an additional check to ensure that the argument is actually a directory and not a file/symlink/char special file/blk special file/FIFO file. You can make use of the st_mode
field of the stat structure
for this.
如果返回值是,0
您需要额外检查以确保参数实际上是一个目录,而不是文件/符号链接/字符特殊文件/blk 特殊文件/FIFO 文件。为此,您可以使用 the 的st_mode
字段stat structure
。
回答by blaze
If you don't really care about type of this filesystem object, access(name, F_OK) checks for exsistence of something with this name. If you need to be sure this is directory, use stat() and check type with S_ISDIR() macro.
如果您并不真正关心此文件系统对象的类型,则 access(name, F_OK) 会检查是否存在具有此名称的内容。如果您需要确定这是目录,请使用 stat() 并使用 S_ISDIR() 宏检查类型。
回答by Aishwary Dewangan
Another simple way would be:
另一种简单的方法是:
int check(unsigned const char type) {
if(type == DT_REG)
return 1;
if(type == DT_DIR)
return 0;
return -1;
}
You can then pass struct dirent*object's d_type to checkfunction.
然后,您可以传递struct dirent*对象的 d_type 来检查函数。
If check returns 1, then that path points to regular file.
如果检查返回 1,则该路径指向常规文件。
If check returns 0, then that path points to a directory.
如果检查返回 0,则该路径指向一个目录。
Otherwise, it is neither a file or a directory (it can be a Block Device/Symbolic Link, etc..)
否则,它既不是文件也不是目录(它可以是块设备/符号链接等。)