- 论坛徽章:
- 0
|
代码如下
#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 编辑 ] |
|