【问题标题】:Why touching "d_name" makes calls to readdir() fail?为什么触摸“d_name”会导致调用 readdir() 失败?
【发布时间】:2010-03-30 12:09:32
【问题描述】:

我正在尝试为 Windows 编写一个小助手,它最终将接受文件扩展名作为参数并返回当前目录中此类文件的数量。

为此,我正在读取目录中的文件条目,并在获取扩展名后将其转换为小写,以便与尚未添加的指定参数进行比较。

在将扩展名转换为小写时,我发现即使触摸 d_name 变量的重复字符串也会导致奇怪的行为,例如不再调用 readdir

这是我现在正在使用的代码(注释代码是初步的)和给定目录的输出:

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

char * strrch(char *string, size_t elements, char character) {
    char *reverse = string + elements;
    while (--reverse != string)
        if (*reverse == character)
            return reverse;
    return NULL;
}

void test(char *string) {
    // Even being a duplicate will make it fail:
    char *str = strdup(string);
    printf("Strings: %s %s\n", string, str);
    *str = 'a';
    printf("Strings: %s %s\n", string, str);

    //unsigned short int i = 0;
    //for (; str[i] != '\0', str++; i++)
    //   str[i] = tolower((unsigned char) str[i]);
    //puts(str);
}

int main(int argc, char **argv) {
    DIR *directory;
    struct dirent *element;   

    if (directory = opendir(".")) {
        while (element = readdir(directory))
            test(strrch(element->d_name, element->d_namlen, '.'));
        closedir(directory);
        puts(NULL);
    } else
        puts("Couldn't open the directory.\n");
}

不修改重复输出(修改和第二个printf调用注释):

Strings: (null) (null)
Strings: . .
Strings: .exe .exe
Strings: .pdf .pdf
Strings: .c .c
Strings: .ini .ini
Strings: .pdf .pdf
Strings: .pdf .pdf
Strings: .pdf .pdf
Strings: .flac .flac
Strings: .FLAC .FLAC
Strings: .lnk .lnk
Strings: .URL .URL

同一目录的输出(与上面的代码,带有2个printfs):

Strings: (null) (null)

有什么问题吗?是编译器问题吗?我现在在 Windows (MinGW) 中使用 GCC 4.4.3。

非常感谢您的帮助。

顺便问一下,有没有其他方法可以在不使用 POSIX 函数的 Windows 环境中处理文件和目录?

【问题讨论】:

    标签: c windows file filesystems directory-structure


    【解决方案1】:

    在 Windows 系统上,“本机”API 不是 POSIX,而是 Win32。对于 Win32,请尝试 FindFirstFile()FindNextFile()

    至于您的代码:您的第一行显示对test() 的第一次调用是使用NULL 指针(这就是您的strrch() 函数为"." 的文件名返回的内容)。 strdup() 很友好,不会在 NULL 上崩溃,而是返回 NULL。然后修改不存在的“字符串”。这是一个 NULL 指针取消引用,此时任何事情都会发生。在 Unix 系统上,这意味着应用程序立即终止,并带有段错误。在 Windows 上,这可能取决于操作系统品牌。

    【讨论】:

    • 哦,非常感谢!我没有意识到,如果它崩溃了,它会更明显。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2019-11-26
    • 2010-12-06
    • 2018-03-30
    相关资源
    最近更新 更多