如何确定的父/其它目录中的文件和目录 [英] How to determine files and directories in parent/other directories

查看:129
本文介绍了如何确定的父/其它目录中的文件和目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了答案,这里的另一个问题 来是非常有益的。

有似乎是SYS / stat.h库的限制,当我试图在其他目录都被看作是一个目录来看看。

我想知道是否有人知道另一个系统的功能或者为什么它认为当前的工作目录以外的任何东西。因为只有一个目录

我AP preciate任何帮助任何人所提供的,因为这是困扰我和各种搜索都翻起来没有帮助。

在code我做了测试是这样的:

 的#include< SYS / stat.h>
#包括LT&;&dirent.h GT;
#包括LT&;&stdio.h中GT;诠释主要(无效){        INT状态;        结构统计st_buf;
        结构的dirent * dirInfo;        DIR * selDir;
        selDir =执行opendir(../);
                                    // ^或任何你想看看
        而((dirInfo = READDIR(selDir))){                状态= STAT(dirInfo-> d_name,&安培; st_buf);                如果(S_ISREG(st_buf.st_mode)){
                        的printf(%s是一个普通文件\\ n,dirInfo-> d_name);
                }
                如果(S_ISDIR(st_buf.st_mode)){
                        的printf(%s是一个目录\\ n,dirInfo-> d_name);
                }        }        返回0;}


解决方案

您需要检查的状态STAT 调用;它是失败的。

麻烦的是,你正在寻找当前目录中的文件 the_file 时,它实际上只在 ../ the_file <发现/ code>。在 READDIR()功能使您相对于其他目录的名称,但 STAT()工作WRT当前目录

要使它工作,你需要做的是等效的:

 字符全称[1024];的snprintf(全名,sizeof的(全名),%S /%S,..,dirInfo-&GT; d_name);如果(STAT(全名,和放大器; st_buf)== 0)
    ...成功报表...
其他
    ...对故障报告...

I found the answer to another question here to be very helpful.

There seems to be a limitation of the sys/stat.h library as when I tried to look in other directories everything was seen as a directory.

I was wondering if anyone knew of another system function or why it sees anything outside the current working directory as only a directory.

I appreciate any help anyone has to offer as this is perplexing me and various searches have turned up no help.

The code I made to test this is:

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

int main(void) {

        int status;

        struct stat st_buf;
        struct dirent *dirInfo;

        DIR *selDir;
        selDir = opendir("../");
                                    // ^ or wherever you want to look
        while ((dirInfo = readdir(selDir))) {

                status = stat (dirInfo->d_name, &st_buf);

                if (S_ISREG (st_buf.st_mode)) {
                        printf ("%s is a regular file.\n", dirInfo->d_name);
                }
                if (S_ISDIR (st_buf.st_mode)) {
                        printf ("%s is a directory.\n", dirInfo->d_name);
                }

        }

        return 0;

}

解决方案

You need to check the status of the stat call; it is failing.

The trouble is that you're looking for a file the_file in the current directory when it is actually only found in ../the_file. The readdir() function gives you the name relative to the other directory, but stat() works w.r.t the current directory.

To make it work, you'd have to do the equivalent of:

char fullname[1024];

snprintf(fullname, sizeof(fullname), "%s/%s", "..", dirInfo->d_name);

if (stat(fullname, &st_buf) == 0)
    ...report on success...
else
    ...report on failure...

这篇关于如何确定的父/其它目录中的文件和目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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