在 Debian(以及所有 Linux 系统)中,readdir 本身是支持中文路径的。
下面分几个层面说明:
readdir 与编码的关系readdir(以及 readdir_r)只是 按字节返回目录项名称,它并不关心文件名是什么语言或编码。
readdir 返回的就是 UTF-8 编码的字符串(char *)✅ 所以:只要文件系统使用的是 UTF-8(Debian 默认就是),中文路径完全没问题
通常不是 readdir 不支持,而是:
检查:
locale
正常应看到:
LANG=zh_CN.UTF-8
LC_CTYPE=UTF-8
如果不是 UTF-8,可能导致:
修复:
sudo apt install locales
sudo dpkg-reconfigure locales
选择 zh_CN.UTF-8 或 en_US.UTF-8
❌ 错误示例:
if (strcmp(name, "中文") == 0)
如果源码文件不是 UTF-8,或终端不是 UTF-8,就会失败。
✅ 正确方式:
例如:
iocharset=utf8NTFS 挂载示例:
mount -t ntfs-3g -o utf8 /dev/sdb1 /mnt
#include <dirent.h>
#include <stdio.h>
int main() {
DIR *d = opendir(".");
struct dirent *e;
while ((e = readdir(d))) {
printf("%s\n", e->d_name);
}
closedir(d);
}
如果目录里有中文文件,输出也会是正常中文(前提是终端 UTF-8)。
✅ Debian 的 readdir 支持中文路径
✅ 底层靠 UTF-8 编码保证
❌ 问题通常出在:locale / 终端 / 代码编码 / 文件系统挂载方式
如果你有具体场景(C / C++ / Python / shell / NFS / Docker),我可以给你更针对性的建议。