C语言 检查文件是目录还是文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4553012/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:25:38  来源:igfitidea点击:

Checking if a file is a directory or just a file

cposix

提问by Jookia

I'm writing a program to check if something is a file or is a directory. Is there a better way to do it than this?

我正在编写一个程序来检查某些东西是文件还是目录。有没有比这更好的方法呢?

#include <stdio.h>

#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

int isFile(const char* name)
{
    DIR* directory = opendir(name);

    if(directory != NULL)
    {
     closedir(directory);
     return 0;
    }

    if(errno == ENOTDIR)
    {
     return 1;
    }

    return -1;
}

int main(void)
{
    const char* file = "./testFile";
    const char* directory = "./";

    printf("Is %s a file? %s.\n", file,
     ((isFile(file) == 1) ? "Yes" : "No"));

    printf("Is %s a directory? %s.\n", directory,
     ((isFile(directory) == 0) ? "Yes" : "No"));

    return 0;
}

回答by Frédéric Hamidi

You can call the stat()function and use the S_ISREG()macro on the st_modefield of the statstructure in order to determine if your path points to a regular file:

您可以调用stat()函数并在stat结构S_ISREG()st_mode字段上使用宏,以确定您的路径是否指向常规文件:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int is_regular_file(const char *path)
{
    struct stat path_stat;
    stat(path, &path_stat);
    return S_ISREG(path_stat.st_mode);
}

Note that there are other file types besides regularand directory, like devices, pipes, symbolic links, sockets, etc. You might want to take those into account.

请注意,除了常规目录之外,还有其他文件类型,如设备、管道、符号链接、套接字等。您可能需要考虑这些。

回答by ismail

Use the S_ISDIRmacro:

使用S_ISDIR宏:

int isDirectory(const char *path) {
   struct stat statbuf;
   if (stat(path, &statbuf) != 0)
       return 0;
   return S_ISDIR(statbuf.st_mode);
}

回答by KARASZI István

Yes, there is better. Check the stator the fstatfunction

是的,还有更好的。检查statfstat功能

回答by R.. GitHub STOP HELPING ICE

Normally you want to perform this check atomically with using the result, so stat()is useless. Instead, open()the file read-only first and use fstat(). If it's a directory, you can then use fdopendir()to read it. Or you can try opening it for writing to begin with, and the open will fail if it's a directory. Some systems (POSIX 2008, Linux) also have an O_DIRECTORYextension to openwhich makes the call fail if the name is not a directory.

通常,您希望使用结果以原子方式执行此检查,因此stat()无用。相反,open()文件首先是只读的,然后使用fstat(). 如果它是一个目录,则可以使用fdopendir()它来读取它。或者您可以尝试打开它开始写入,如果它是目录,则打开将失败。某些系统(POSIX 2008、Linux)也有一个O_DIRECTORY扩展名,open如果名称不是目录,则调用失败。

Your method with opendir()is also good if you want a directory, but you should not close it afterwards; you should go ahead and use it.

opendir()如果你想要一个目录,你的方法也很好,但之后你不应该关闭它;你应该继续使用它。