libiconv_sample.c

#include <stdio.h>
#include <malloc.h>
#include "libiconv/iconv.h"
#ifdef _DEBUG
#pragma comment(lib, "libiconv/libiconvd.lib")
#else
#pragma comment(lib, "libiconv/libiconv.lib")
#endif
#define BUFFER_SIZE 10 * 1024 * 1024
int main()
{
const char *toCode = "GBK", *fromCode = "UTF-8";
iconv_t conversionDescriptor = iconv_open(toCode, fromCode);
if ((iconv_t)- == conversionDescriptor)
{
if (errno == EINVAL)
printf("Not supported from %s to %s.\n", fromCode, toCode);
else
printf("Unknown error.\n");
}
else
{
const char *filename = "text_utf-8.txt";
FILE *inputFile = fopen(filename, "r");
if (inputFile)
{
filename = "text_gbk.txt";
FILE *outputFile = fopen(filename, "w");
if (outputFile)
{
char *sourceBuffer = (char *)malloc(sizeof(char) * BUFFER_SIZE);
const char *sourcePtr = sourceBuffer;
size_t destinationBufferSize = BUFFER_SIZE * , availableSpaceOfDestinationBuffer = destinationBufferSize;
char *destinationBuffer = (char *)malloc(sizeof(char) * destinationBufferSize), *destinationPtr = destinationBuffer;
size_t numberOfCharactersRead = fread(sourceBuffer, sizeof(char), BUFFER_SIZE, inputFile);
iconv(conversionDescriptor, &sourcePtr, &numberOfCharactersRead, &destinationPtr, &availableSpaceOfDestinationBuffer);
size_t characterCount = destinationBufferSize - availableSpaceOfDestinationBuffer;
destinationBuffer[characterCount] = '\0';
printf(destinationBuffer);
fwrite(destinationBuffer, sizeof(char), characterCount, outputFile);
free(sourceBuffer);
free(destinationBuffer);
fclose(outputFile);
}
else
printf("Cannot open file: %s.\n", filename);
fclose(inputFile);
}
else
printf("Cannot open file: %s.\n", filename);
iconv_close(conversionDescriptor);
}
system("pause");
return ;
}

参考链接:unnonouno/iconvpp: wrapper library of iconv for c++

最新文章

  1. 国内maven镜像,快的飞起
  2. iNeedle产品介绍
  3. GUN485项目的总结
  4. 支持多人协作的在线免费作图工具:ProcessOn
  5. 用css3写出来的进度条
  6. Pick-up sticks - POJ 2653 (线段相交)
  7. 正则如何匹配div下的所有&lt;li&gt;标签?
  8. SharePoint 2016 每天预热脚本介绍
  9. Server.MapPath找不到命名空间,解决办法
  10. 一篇迟到的gulp文章
  11. Load data local infile 实验报告
  12. Core Expression
  13. webapi快速开发框架
  14. Python之禅的翻译和解释
  15. java解惑--摘要
  16. WebStrom2018注册码
  17. C++ new运算符
  18. LuoguP2680 运输计划
  19. [Unity优化]UI优化(一):RaycastTarget
  20. mysql \G

热门文章

  1. POJ——2251Dungeon Master(三维BFS)
  2. [POJ1143]Number Game
  3. BZOJ3261 最大异或和 【可持久化trie树】
  4. 刷题总结——书架(bzoj1861)
  5. 论文笔记:Ten years of pedestrian detection, what have we learned?
  6. P1651 塔 (动态规划)
  7. Java中NIO、BIO、AIO相关概念及应用场景
  8. bzoj2324 [ZJOI2011]营救皮卡丘 费用流
  9. 【bzoj4004】【JLOI2015】装备购买 (线性基+高斯消元)
  10. UVa1363 Joseph&#39;s Problem