主题:[讨论]linux下的iconv转码问题 (急)
想用C语言编写xml解析文件,要求把gb2312转为utf-8,使用了linux下的iconv进行转化,但是总是返回-1;无法转码,不知道是那里出问题,请大虾们帮忙,很急!
我的系统是red hat 9.0, 编译器是gcc。
下面是代码:
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <string.h>
int main()
{
iconv_t cd;
char instr[]="汉字";
char *inbuf;
char *outbuf;
char *outptr;
unsigned int insize=strlen(instr);
unsigned int outputbufsize=10;
unsigned int avail=outputbufsize;
unsigned int nconv;
FILE *pf;
inbuf=instr;
outbuf=(char *)malloc(outputbufsize);
outptr=outbuf;
memset(outbuf,'\0',outputbufsize);
cd=iconv_open("utf-8","gb2312"); //将字符串编码由gb2312转换为utf-8
if(cd==(iconv_t)-1)
{
printf("fail.\n");
}
nconv=iconv(cd,&inbuf,&insize,&outptr,&avail);//nconv总返回 -1
printf("%s\n",outbuf);
//write xml file
pf = fopen("test.xml","wb");
if(!pf)
return 0;
strcpy(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "<Image FileName=\"test.xml\" OCRStatus=\"test\">\n");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "<Return Code =\"%d\">\n",res);
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf,"\t<![CDATA[");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf,"%s",outbuf);
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "\t]]>\n");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "</Return>\n");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf (buf, "</Image>\n");
fwrite(buf, 1, strlen(buf), pf);
free(outbuf);
iconv_close(cd);
fclose(pf);
return 1;
}
我的系统是red hat 9.0, 编译器是gcc。
下面是代码:
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <string.h>
int main()
{
iconv_t cd;
char instr[]="汉字";
char *inbuf;
char *outbuf;
char *outptr;
unsigned int insize=strlen(instr);
unsigned int outputbufsize=10;
unsigned int avail=outputbufsize;
unsigned int nconv;
FILE *pf;
inbuf=instr;
outbuf=(char *)malloc(outputbufsize);
outptr=outbuf;
memset(outbuf,'\0',outputbufsize);
cd=iconv_open("utf-8","gb2312"); //将字符串编码由gb2312转换为utf-8
if(cd==(iconv_t)-1)
{
printf("fail.\n");
}
nconv=iconv(cd,&inbuf,&insize,&outptr,&avail);//nconv总返回 -1
printf("%s\n",outbuf);
//write xml file
pf = fopen("test.xml","wb");
if(!pf)
return 0;
strcpy(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "<Image FileName=\"test.xml\" OCRStatus=\"test\">\n");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "<Return Code =\"%d\">\n",res);
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf,"\t<![CDATA[");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf,"%s",outbuf);
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "\t]]>\n");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "</Return>\n");
fwrite(buf, 1, strlen(buf), pf);
memset(buf, 0, sizeof(buf));
sprintf (buf, "</Image>\n");
fwrite(buf, 1, strlen(buf), pf);
free(outbuf);
iconv_close(cd);
fclose(pf);
return 1;
}