Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从文件夹中检索文件的属性_C_Readdir - Fatal编程技术网

C 从文件夹中检索文件的属性

C 从文件夹中检索文件的属性,c,readdir,C,Readdir,我正在做一项作业,要求我创建一个类似ls的函数。 我的代码工作得很好,但在实现 ls -l child //where child is a folder 有一种奇怪的行为 假设我在“parent”文件夹中,其中有一个子文件夹“child”,其中包含一些文本文件。当我从父文件夹运行程序时,它会找到该文件夹并打印其中文本文件的属性。但是,仅当父文件夹中存在相同的文件时,它才会打印子文件夹中的文件 下面是我正在使用的代码片段 char CurrDir[100]; DIR *pDir

我正在做一项作业,要求我创建一个类似ls的函数。 我的代码工作得很好,但在实现

ls -l child //where child is a folder
有一种奇怪的行为

假设我在“parent”文件夹中,其中有一个子文件夹“child”,其中包含一些文本文件。当我从父文件夹运行程序时,它会找到该文件夹并打印其中文本文件的属性。但是,仅当父文件夹中存在相同的文件时,它才会打印子文件夹中的文件

下面是我正在使用的代码片段

    char CurrDir[100];
    DIR *pDir = NULL;
    struct dirent *pFileNames = NULL;

    getcwd(CurrDir, sizeof(CurrDir))

    strncat(CurrDir, "/", strlen(CurrDir));

    unsigned int CurrDirLen = strlen(CurrDir);
    unsigned int CombSize = CurrDirLen + strlen(argv[1]);

    char SuperCharArr[CombSize];

    for(int i = 0; i < CombSize; ++i)
    {
        if( i < strlen(CurrDir) )
            SuperCharArr[i] = CurrDir[i];
        else
            SuperCharArr[i] = argv[1][i%CurrDirLen];
    }//for

    //insert null character at the end of the character
    SuperCharArr[CombSize] = '\0';

    pDir = opendir( SuperCharArr );
    printf("%s\n", SuperCharArr);

    if( pDir != NULL )
    {
        //Directory detected as pDir is a DirectoryStream
        printf("%s\n", "pDir not null");
        PrintHeader();

        while( (pFileNames = readdir(pDir)) != NULL )
        {
            PrintFileDeails(pFileNames);
        }
    }//if
charcurrdir[100];
DIR*pDir=NULL;
struct dirent*pFileNames=NULL;
getcwd(CurrDir,sizeof(CurrDir))
strncat(CurrDir,“/”,strlen(CurrDir));
无符号整数CurrDirLen=strlen(CurrDir);
无符号整数CombSize=currdirelen+strlen(argv[1]);
字符超字符[CombSize];
对于(int i=0;i
在我发布的原始代码中,有一个名为PrintFileDeails(pFileNames)的函数,它接受一个direct类型的参数

在PrintFileDeails()中,有一个函数检查文件的状态,代码如下:

struct stat FileStat;

if( stat(pFileNames->d_name, &FileStat) == -1 )
{
    perror("stat");
    exit(EXIT_FAILURE);
}//if
这行代码会打印出一个错误,他们找不到文件,AAT的评论让我再次检查代码,因为我怀疑它没有读取正确的文件夹。因此,在我通过了它应该从哪里读取文件的完整路径之后,它工作得很好。因此,代码改为此

if( stat(pFullPath, &FileStat) == -1 )
{
    perror("stat");
    exit(EXIT_FAILURE);
}//if
其中pFullPath被传递给supercharar变量,该变量包含要搜索的文件所在位置的完整路径


stat()的手册页也有帮助,可以找到它

我没有使用过
readdir()
,但我想它只返回文件名,而不是完整路径。换句话说,您需要先使用
SuperCharArr
路径名限定
pFileNames->d_name
,然后才能使用它。@AAT是的,但在每个循环中,它都会返回您正在读取的目录中每个文件的文件名及其属性,在本例中,该目录由pDir指针表示。不过,谢谢你的建议。等等,你的评论让我检查了一些事情,我认为你是对的。它正在错误的路径中检查。至少在我的函数PrintFileDeails()中是这样的。谢谢你,我很快就会发布正确答案。很高兴能够提供帮助!您还可以使用dirfd()+fstatat()代替stat()'来填充完整路径。出于各种原因,它会更好,但有些新,所以不太便于携带。