opendir 本身是 POSIX 标准函数,在 Ubuntu(Linux)上是 支持中文目录名的,前提是满足几个条件:
中文目录),opendir("中文目录") 是可以正常打开的DIR *dir = opendir("中文目录");
在 UTF-8 环境下是 OK 的确保 locale 支持 UTF-8:
locale
应看到类似:
LANG=zh_CN.UTF-8
LC_ALL=zh_CN.UTF-8
如果不是,可以:
sudo apt install locales
sudo locale-gen zh_CN.UTF-8
❌ 不支持的情况:
opendir✅ 解决方法:
convmv -f gbk -t utf8 --notest 中文目录
iconv 转换路径字符串#include <dirent.h>
#include <stdio.h>
int main() {
DIR *dir = opendir("测试目录");
if (!dir) {
perror("opendir");
return 1;
}
printf("打开成功\n");
closedir(dir);
return 0;
}
✅ 总结:Ubuntu 的 opendir 完全支持中文,只要使用 UTF-8 编码即可。
如果你遇到具体报错,可以把错误信息和代码贴出来。