我正在尝试为 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 个 printf
):
Strings: (null) (null)
有什么问题吗?这是编译器问题吗?我现在在 Windows (MinGW) 中使用 GCC 4.4.3。
非常感谢您的帮助。
顺便问一下,有没有其他方法可以在 Windows 环境中不使用 POSIX 函数来处理文件和目录?
最佳答案
在 Windows 系统上,“本地”API 不是 POSIX,而是 Win32。使用 Win32,尝试 FindFirstFile()和 FindNextFile() .
至于您的代码:您的第一行显示对 test()
的第一次调用是使用 NULL 指针(这就是您的 strrch()
函数返回的内容"."
的文件名)。 strdup()
不会在 NULL
上崩溃,而是返回 NULL
。然后你修改不存在的“字符串”。这是一个 NULL 指针取消引用,此时任何事情都会发生。在 Unix 系统上,这意味着应用程序立即终止,并出现段错误。在 Windows 上,这可能取决于操作系统品牌。
关于c - 为什么触摸 "d_name"会使对 readdir() 的调用失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2544949/