区分 C 和 C 中的 unix 目录和文件++

发布于 2024-07-24 21:35:00 字数 80 浏览 2 评论 0原文

给定一个路径,例如 /home/shree/path/def,我想确定 def 是目录还是文件。 有没有办法用 C 或 C++ 代码实现这一点?

Given a path, say, /home/shree/path/def, I would want to determine if def is a directory or a file. Is there a way of achieving this in C or C++ code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜扑 2024-07-31 21:35:00

以下代码使用 stat() 函数以及 S_ISDIR(“是目录”)和 S_ISREG(“是常规文件”)宏来获取文件信息。 剩下的只是错误检查,足以制作一个完整的可编译程序。

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main (int argc, char *argv[]) {
    int status;
    struct stat st_buf;

    // Ensure argument passed.

    if (argc != 2) {
        printf ("Usage: progName <fileSpec>\n");
        printf ("       where <fileSpec> is the file to check.\n");
        return 1;
    }

    // Get the status of the file system object.

    status = stat (argv[1], &st_buf);
    if (status != 0) {
        printf ("Error, errno = %d\n", errno);
        return 1;
    }

    // Tell us what it is then exit.

    if (S_ISREG (st_buf.st_mode)) {
        printf ("%s is a regular file.\n", argv[1]);
    }
    if (S_ISDIR (st_buf.st_mode)) {
        printf ("%s is a directory.\n", argv[1]);
    }

    return 0;
}

示例运行如下所示:


pax> vi progName.c ; gcc -o progName progName.c ; ./progName
Usage: progName 
       where  is the file to check.

pax> ./progName /home
/home is a directory.

pax> ./progName .profile
.profile is a regular file.

pax> ./progName /no_such_file
Error, errno = 2

The following code uses the stat() function and the S_ISDIR ('is a directory') and S_ISREG ('is a regular file') macros to get information on the file. The rest is just error checking and enough to make a complete compilable program.

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main (int argc, char *argv[]) {
    int status;
    struct stat st_buf;

    // Ensure argument passed.

    if (argc != 2) {
        printf ("Usage: progName <fileSpec>\n");
        printf ("       where <fileSpec> is the file to check.\n");
        return 1;
    }

    // Get the status of the file system object.

    status = stat (argv[1], &st_buf);
    if (status != 0) {
        printf ("Error, errno = %d\n", errno);
        return 1;
    }

    // Tell us what it is then exit.

    if (S_ISREG (st_buf.st_mode)) {
        printf ("%s is a regular file.\n", argv[1]);
    }
    if (S_ISDIR (st_buf.st_mode)) {
        printf ("%s is a directory.\n", argv[1]);
    }

    return 0;
}

Sample runs are shown here:


pax> vi progName.c ; gcc -o progName progName.c ; ./progName
Usage: progName 
       where  is the file to check.

pax> ./progName /home
/home is a directory.

pax> ./progName .profile
.profile is a regular file.

pax> ./progName /no_such_file
Error, errno = 2
第七度阳光i 2024-07-31 21:35:00

使用 stat(2) 系统调用。 您可以在 st_mode 字段上使用 S_ISREG 或 S_ISDIR 宏来查看给定路径是文件还是目录。 手册页告诉您所有其他字段。

Use the stat(2) system call. You can use the S_ISREG or S_ISDIR macro on the st_mode field to see if the given path is a file or a directory. The man page tells you about all the other fields.

噩梦成真你也成魔 2024-07-31 21:35:00

使用 boost::filesystem 库及其 is_directory(const Path& p) 怎么样? 可能需要一段时间才能熟悉,但也不会太久。 它可能值得投资,并且您的代码不会特定于平台。

What about using the boost::filesystem library and its is_directory(const Path& p) ? It may take a while to get familiar with, but not so much. It probably worths the investment, and your code will not be platform specific.

爱你是孤单的心事 2024-07-31 21:35:00

或者,您可以将 system() 函数与内置 shell 命令“test”一起使用。
系统返回最后执行的命令的退出状态

 
 string test1 = "test -e filename" ;
 if(!system(test1))
 printf("filename exists") ;

string test2 = "test -d filename" ;
 if(!system(test2))
  printf("filename is a directory") ;

 string test3 = "test -f filename" ;
 if(!system(test3))
  printf("filename is a normal file") ;

但恐怕这只能在Linux上工作。

Alternatively you can use system() function with in built shell command "test".
system returns the exit status of command last executed

 
 string test1 = "test -e filename" ;
 if(!system(test1))
 printf("filename exists") ;

string test2 = "test -d filename" ;
 if(!system(test2))
  printf("filename is a directory") ;

 string test3 = "test -f filename" ;
 if(!system(test3))
  printf("filename is a normal file") ;

but I am afraid this would work only on linux..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文