免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1763 | 回复: 6
打印 上一主题 下一主题

玩不转 iconv ,求助【已解决】 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-25 13:01 |只看该作者 |倒序浏览
代码如下

#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>

int main(int argc, char *argv[])
{
        FILE *fd = fopen(argv[1], "r");
        if(fd == NULL)
        {
                printf("E:Open %s failed.\n", argv[1]);
        }
        fseek(fd, 0, SEEK_END);
        int content_size = ftell(fd);
        fseek(fd, 0, SEEK_SET);
        printf("File size = %d\n", content_size);

    char *content = malloc(content_size +1);
    if(NULL == content)
    {
        printf("E:Call to malloc failed.\n");
        return 1;
    }
    bzero(content, content_size);
    fread( content, content_size, 1, fd);
    
    iconv_t cd = iconv_open("utf-8", "gbk");
    if(cd == (iconv_t) -1)
    {
        printf("E:Call to iconv_open failed.\n");
        return 1;
    }
    char *output = malloc(500);
    if(NULL == output)
    {
        printf("E:Call to malloc failed.\n");
        return 1;
    }
    bzero(output, 500);
    unsigned int avail = 500;
    unsigned int nconv = 0;

    if(-1 == iconv(cd, &content, (size_t *)&content_size, &output, (size_t *)&avail))
    {
        printf("E:Call to iconv failed.\n");
        return 1;
    }
    
    fd = fopen("/home/zhou/output", "w");
    if(NULL == fd)
    {
        printf("E:Call to fopen failed.\n");
        return 1;
    }
    fwrite(output, strlen(output), 1, fd);
    fclose(fd);
    
    return 0;
}



输入 ANSI 编码的txt文件,没有输出

[ 本帖最后由 daschina 于 2010-1-25 13:46 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2010-01-25 13:16 |只看该作者

iconv_open("utf-8", "gb2312");

试试?

论坛徽章:
0
3 [报告]
发表于 2010-01-25 13:19 |只看该作者
原帖由 rain_fish 于 2010-1-25 13:16 发表

iconv_open("utf-8", "gb2312");

试试?



刚试了一下,没用

论坛徽章:
0
4 [报告]
发表于 2010-01-25 13:32 |只看该作者
不知道你是怎么用的,看看这个,我用的是Unicode到gb2312的转换

//代码转换:从一种编码转为另一种编码
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,unsigned char *outbuf,int& outlen)
{
        iconv_t cd;

        char **pin = &inbuf;
        unsigned char **pout = &outbuf;
        size_t len_tmp = outlen;
       
        cd = iconv_open(to_charset,from_charset);
        if (cd==0) return -1;
        memset(outbuf,0,outlen);
        if ( iconv(cd,pin,(size_t*)&inlen,(char**)pout,&len_tmp) == -1 )
                return -1;
        iconv_close(cd);

        outlen = outlen - len_tmp;//编码后的数据长度
        return 0;
}
//UNICODE码转为GB2312码
int u2g(char *inbuf,int inlen,unsigned char *outbuf,int& outlen)
{
        return code_convert("UNICODE","gb2312",inbuf,inlen,outbuf,outlen);
}

论坛徽章:
0
5 [报告]
发表于 2010-01-25 13:35 |只看该作者
iconv -c -f utf-8 -t gb2312 file_utf-8 > file_gb2312

论坛徽章:
0
6 [报告]
发表于 2010-01-25 13:38 |只看该作者
搞定了,原来

#include <iconv.h>

size_t iconv(iconv_t cd,
char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft);

这个函数转换玩了之后 outbuf 已经指到末尾了,被改变了,所以传递的时候这样

char *temp = outbuf;
iconv(td, &inbuf, in_size, &temp, out_size);

然后读 outbuf 就可以了

论坛徽章:
0
7 [报告]
发表于 2010-01-25 13:46 |只看该作者
结贴咯
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP