READDIR()用点而不是文件开始 [英] readdir() beginning with dots instead of files

查看:127
本文介绍了READDIR()用点而不是文件开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。我在读从目录中的文件和它的作品,但它在开始读取两个额外的文件...是什么呢?
例如,有一个文件列表:A348,A348A,A348B
这就是我所得到的:,..,A348,A348A,A348B ???

  DIR * DIR;
结构的dirent * DP;
字符* FILE_NAME;
而((DP = READDIR(DIR))!= NULL){        FILE_NAME = DP-GT&; d_name;
}


解决方案

是当前目录的目录条目

.. 是目录起来层次一个层次

目录项

您必须只筛选出来使用:

(!STRCMP(DP-GT&; d_name,)|| STRCMP(DP-GT与!; d_name,))

 如果
{
     //什么也不做(直逻辑)
}其他{
     FILE_NAME = DP-GT&; d_name; // 用它
}

更多关于使用 .. 在Windows上:

\\\\文件。 - 这是一个名为文件在当前工作目录

.. \\\\文件 - 这是在父目录中的文件

.. \\\\ \\\\ otherdir文件 - 这是一个文件,是在指定目录 otherdir 即在相同的水平的当前目录(我们不必知道什么目录是我们在)。

编辑:READDIR的整装用法示例:

 的#include<&stdio.h中GT;
#包括LT&;&dirent.h GT;诠释的main()
{
    DIR * DIR;
    结构的dirent * DP;
    字符* FILE_NAME;
    DIR =执行opendir(。);
    而((DP = READDIR(DIR))!= NULL){
        的printf(调试:%S \\ n,DP-GT&; d_name);
        (!STRCMP(DP-GT&; d_name,)|| STRCMP(DP-GT与!; d_name,))如果
        {
            //什么也不做(直逻辑)
        }其他{
            FILE_NAME = DP-GT&; d_name; // 用它
            的printf(FILE_NAME:\\%s \\的\\ n,FILE_NAME);
        }
    }
    closedir(DIR);
    返回0;
}

I have a little problem. I'm reading files from directory and it works, but it read two extra files on the beginning ...what is it? for example, there is a list of files: "A348", "A348A", "A348B" and this is what i get: ".", "..", "A348", "A348A", "A348B" ???

DIR *dir;
struct dirent *dp;
char * file_name;
while ((dp=readdir(dir)) != NULL) {

        file_name = dp->d_name;            
}

解决方案

. is a directory entry for current directory

.. is a directory entry for the directory one level up in hierarchy

You have to just filter them out using:

if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") )
{
     // do nothing (straight logic)
} else {
     file_name = dp->d_name; // use it
}

More on using . and .. on Windows:

".\\file" - this is a file named file in current working directory

"..\\file" - this is a file in a parent directory

"..\\otherdir\\file" - this is a file that is in directory named otherdir, that is at the same level as current directory (we don't have to know what directory are we in).

Edit: selfcontained example usage of readdir:

#include <stdio.h>
#include <dirent.h>

int main()
{
    DIR *dir;
    struct dirent *dp;
    char * file_name;
    dir = opendir(".");
    while ((dp=readdir(dir)) != NULL) {
        printf("debug: %s\n", dp->d_name);
        if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") )
        {
            // do nothing (straight logic)
        } else {
            file_name = dp->d_name; // use it
            printf("file_name: \"%s\"\n",file_name);
        }
    }
    closedir(dir);
    return 0;
}

这篇关于READDIR()用点而不是文件开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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