gpt4 book ai didi

c - 有没有办法让 dirent 的 ent->d_name 作为 w_char * ?

转载 作者:行者123 更新时间:2023-11-30 17:13:43 25 4
gpt4 key购买 nike

我使用 dirent 生成文件列表,但我担心包含 unicode 字符的目录和文件。

void recurse_dir(char *dir)
{
DIR* d;
d = opendir(dir);
struct dirent* ent;
unsigned short int dir_size = strlen(dir), tmp_dir_size;
if(d != NULL)
{
while((ent = readdir(d)) != NULL)
{
if(ent->d_type == DT_DIR)
{
if(!strcmp(ent->d_name,".") || !strcmp(ent->d_name,".."))
continue;
folder_count++;
char tmp_dir[dir_size + strlen(ent->d_name) + 2];
tmp_dir[0] = '\0';
strcat(tmp_dir,dir);
strcat(tmp_dir,"/");
strcat(tmp_dir,ent->d_name);
recurse_dir(tmp_dir);
}
else
{
file_count++;
file_strs_size += dir_size + strlen(ent->d_name) + 2;
fprintf(list_fp, "%s/%s\n",dir, ent->d_name);
}
}
}
closedir(d);
}

有没有办法可以获取宽字符串格式的 ent->d_name?

最佳答案

您可以使用 UTF-8 格式将所有 Unicode 字符存储在 char 数组中。这可能是您的操作系统存储该名称的方式,因此如果您想要 UTF-16 或 UTF-32 格式的名称,您可以使用负责此操作的函数进行转换,例如 iconv .

关于c - 有没有办法让 dirent 的 ent->d_name 作为 w_char * ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30645010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com