| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2374 人关注过本帖
标题:求助。如何更改readdir函数读出的目录流编码?
只看楼主 加入收藏
pyf_3721
Rank: 1
等 级:新手上路
帖 子:26
专家分:4
注 册:2013-3-10
结帖率:100%
收藏
已结贴  问题点数:16 回复次数:12 
求助。如何更改readdir函数读出的目录流编码?
在用C语言编译后的目录列表中,对gb2312码的中文目录和中文文件名显示乱码,我试着将de->d_name更改编码不成功(注:已有UTF-8中文字库),如何才能正确显示中文目录和中文文件名。
搜索更多相关主题的帖子: 中文目录 中文字库 C语言 如何 
2013-03-16 09:21
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:5 
代码

DO IT YOURSELF !
2013-03-16 10:09
pyf_3721
Rank: 1
等 级:新手上路
帖 子:26
专家分:4
注 册:2013-3-10
收藏
得分:0 
char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
{
    char path[PATH_MAX] = "";
    DIR *dir;
    struct dirent *de;
    int total = 0;
    int i;
    char** files = NULL;
    int pass;
    *numFiles = 0;
    int dirLen = strlen(directory);
    dir = opendir(directory);
    if (dir == NULL) {
        ui_print("不能打开目录.\n");
        return NULL;
    }
    int extension_length = 0;
    if (fileExtensionOrDirectory != NULL)
        extension_length = strlen(fileExtensionOrDirectory);
    int isCounting = 1;
    i = 0;
    for (pass = 0; pass < 2; pass++) {
        while ((de=readdir(dir)) != NULL) {
            // skip hidden files
            if (de->d_name[0] == '.')
                continue;
            // NULL means that we are gathering directories, so skip this
            if (fileExtensionOrDirectory != NULL)
            {
                // make sure that we can have the desired extension (prevent seg fault)
                if (strlen(de->d_name) < extension_length)
                    continue;
                // compare the extension
                if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
                    continue;
            }
            else
            {
                struct stat info;
                char fullFileName[PATH_MAX];
                strcpy(fullFileName, directory);
                strcat(fullFileName, de->d_name);
                stat(fullFileName, &info);
                // make sure it is a directory
                if (!(S_ISDIR(info.st_mode)))
                    continue;
            }
            if (pass == 0)
            {
                total++;
                continue;
            }
            files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
            strcpy(files[i], directory);
            strcat(files[i], de->d_name);
            if (fileExtensionOrDirectory == NULL)
                strcat(files[i], "/");
            i++;
        }
        if (pass == 1)
            break;
        if (total == 0)
            break;
        rewinddir(dir);
        *numFiles = total;
        files = (char**) malloc((total+1)*sizeof(char*));
        files[total]=NULL;
    }
    if(closedir(dir) < 0) {
        LOGE("Failed to close directory.");
    }
    if (total==0) {
        return NULL;
    }
    // sort the result
    if (files != NULL) {
        for (i = 0; i < total; i++) {
            int curMax = -1;
            int j;
            for (j = 0; j < total - i; j++) {
                if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
                    curMax = j;
            }
            char* temp = files[curMax];
            files[curMax] = files[total - i - 1];
            files[total - i - 1] = temp;
        }
    }
    return files;
}
// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
{
    char path[PATH_MAX] = "";
    DIR *dir;
    struct dirent *de;
    int numFiles = 0;
    int numDirs = 0;
    int i;
    char* return_value = NULL;
    int dir_len = strlen(directory);
    char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
    char** dirs = NULL;
    if (fileExtensionOrDirectory != NULL)
        dirs = gather_files(directory, NULL, &numDirs);
    int total = numDirs + numFiles;
    if (total == 0)
    {
        ui_print("没有找到文件.\n");
    }
    else
    {
        char** list = (char**) malloc((total + 1) * sizeof(char*));
        list[total] = NULL;

        for (i = 0 ; i < numDirs; i++)
        {
            list[i] = strdup(dirs[i] + dir_len);
        }
        for (i = 0 ; i < numFiles; i++)
        {
            list[numDirs + i] = strdup(files[i] + dir_len);
        }
        for (;;)
        {
            int chosen_item = get_menu_selection(headers, list, 0, 0);
            if (chosen_item == GO_BACK)
                break;
            static char ret[PATH_MAX];
            if (chosen_item < numDirs)
            {
                char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
                if (subret != NULL)
                {
                    strcpy(ret, subret);
                    return_value = ret;
                    break;
                }
                continue;
            }
            strcpy(ret, files[chosen_item - numDirs]);
            return_value = ret;
            break;
        }
        free_string_array(list);
    }
    free_string_array(files);
    free_string_array(dirs);
    return return_value;
}

void gbkutf(char *gbkk)
{            str_normalize_init();
         //   char *gbk;
            uint32_t gbk_len = strlen(gbkk);
            uint32_t utf8buffer_len = gbk_len * 3 + 1;
            char *utf8buffer = (char *)malloc(utf8buffer_len);
            memset(utf8buffer, 0, utf8buffer_len);
            gbk_to_utf8(gbkk, gbk_len, &utf8buffer, &utf8buffer_len);
            strcpy(gbkk, utf8buffer);
            free(utf8buffer);
//            return *utf8buffer;
}
以上即是列表目录代码,语言是在ARM上编译,读中文目录和文件名出乱码(即显示??号)。我编了一个转码函数gbkutf(char *gbk),加在files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
            strcpy(files[i], directory);
            strcat(files[i], de->d_name);------》strcat(files[i], gbkutf(de->d_name));------》运行后,选择菜单(列目录),一闪跳出。加在其它地方不起作用,仍然显示(??)。

[ 本帖最后由 pyf_3721 于 2013-3-16 12:45 编辑 ]
2013-03-16 10:52
小xiong
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:388
专家分:1722
注 册:2013-2-8
收藏
得分:5 
畦,
2013-03-16 14:41
pyf_3721
Rank: 1
等 级:新手上路
帖 子:26
专家分:4
注 册:2013-3-10
收藏
得分:0 
以下是引用小xiong在2013-3-16 14:41:39的发言:

畦,
别哇啊!给点实际的!
2013-03-16 15:00
pyf_3721
Rank: 1
等 级:新手上路
帖 子:26
专家分:4
注 册:2013-3-10
收藏
得分:0 
其中的directory、de->d_name是什么类型?为什么用gbkutf(char *gbk)函数会不向下执行?急!期望解答,整个就差它了,郁闷!

[ 本帖最后由 pyf_3721 于 2013-3-16 17:16 编辑 ]
2013-03-16 17:14
pyf_3721
Rank: 1
等 级:新手上路
帖 子:26
专家分:4
注 册:2013-3-10
收藏
得分:0 
等待中。。。。。。
2013-03-16 19:05
pyf_3721
Rank: 1
等 级:新手上路
帖 子:26
专家分:4
注 册:2013-3-10
收藏
得分:0 
继续期待中。。。。
2013-03-17 10:21
pyf_3721
Rank: 1
等 级:新手上路
帖 子:26
专家分:4
注 册:2013-3-10
收藏
得分:0 
求解决中。。。。
2013-03-17 19:27
shmilyflf
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:5
帖 子:356
专家分:1008
注 册:2012-12-9
收藏
得分:5 
蹭分中……
2013-03-17 19:47
快速回复:求助。如何更改readdir函数读出的目录流编码?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035705 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved