免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: PassionUranus
打印 上一主题 下一主题

[C++] 关于编码转换iconv_open("UTF-8", "GBK")失败 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-08-01 09:33 |只看该作者
问题解决了,我是看了头文件里的解释, 再配合各位的分析解决的。
/* We would like to #include any system header file which could define
   iconv_t, 1. in order to eliminate the risk that the user gets compilation
   errors because some other system header file includes /usr/include/iconv.h
   which defines iconv_t or declares iconv after this file, 2. when compiling
   for LIBICONV_PLUG, we need the proper iconv_t type in order to produce
   binary compatible code.
   But gcc's #include_next is not portable. Thus, once libiconv's iconv.h
   has been installed in /usr/local/include, there is no way any more to
   include the original /usr/include/iconv.h. We simply have to get away
   without it.
   Ad 1. The risk that a system header file does
   #include "iconv.h"  or  #include_next "iconv.h"
   is small. They all do #include <iconv.h>.
   Ad 2. The iconv_t type is a pointer type in all cases I have seen. (It
   has to be a scalar type because (iconv_t)(-1) is a possible return value
   from iconv_open().) */

/* Define iconv_t ourselves. */
#undef iconv_t

论坛徽章:
0
12 [报告]
发表于 2008-08-01 09:39 |只看该作者
我就属于Ad2的情况,我编译的时候指向了系统自带的库,呵呵

论坛徽章:
0
13 [报告]
发表于 2009-04-05 18:33 |只看该作者

回复 #4 mo_yuan_ming 的帖子

他发的代码有问题,如果转换失败之后,就没有释放指针。正常的代码应该如下:

    int ConvertEnc( const char *lpszFrom, const char *lpszTo, const char* lpszSrc, int nSrcLen, char* lpszDest, int* lpnDestLen)
    {
        iconv_t cv;

        if ((cv = iconv_open(lpszTo, lpszFrom)) == (iconv_t)-1)
        {
            return -1;
        }
        iconv(cv, NULL, NULL, NULL, NULL);

        char* lpszSrc1 = (char*)lpszSrc;
        int ret = iconv(cv, &lpszSrc1, (size_t *)&nSrcLen, &lpszDest, (size_t *)lpnDestLen);
        if (ret == -1)
        {
            iconv_close(cv);
            return -1;
        }
        iconv_close(cv);

        return 0;
    }

论坛徽章:
0
14 [报告]
发表于 2010-03-24 16:03 |只看该作者
iconv(cv, NULL, NULL, NULL, NULL);的意思是把iconv描述符的状态置为初始状态,这里没必要用吧?

论坛徽章:
0
15 [报告]
发表于 2010-03-24 16:07 |只看该作者
{:3_190:}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP