- 论坛徽章:
- 0
|
因业务需要,需要把HP机器上的文本(ASCII码形式,对应iso81) 转化成AIX机器上的文本 ebcd码。
函数据说是用convert(tocode, fromcode, Input)
请问:函数中的Input---- 指iint 型的nput file descriptor,输入文件描述符。这是啥东东,百思不得其解。
那位高手能给俺个转换程序参考一下,呵呵,感谢。下面这个编译不过去,偶准备推倒重来,叹息。
#include <iconv.h>;
#include <errno.h>;
char str[1000],strout[1000];
void main()
{
int fd=1;
...
printf("fopen file succ,put str....\n" ;
getchar();
gets(str);
convert('iso81','engle', fd);
......
}
int convert(tocode, fromcode, Input)
{
/*char *tocode; tocode name */
/* char *fromcode; fromcode name */
/* int Input; input file descriptor */
extern void error(); /* local error message */
iconv_t cd; /* conversion descriptor */
unsigned char *table; /* ptr to translation table */
int bytesread; /* num bytes read into input buffer */
unsigned char inbuf[BUFSIZ]; /* input buffer */
unsigned char *inchar; /* ptr to input character */
int inbytesleft; /* num bytes left in input buffer */
unsigned char outbuf[BUFSIZ]; /* output buffer */
unsigned char *outchar; /* ptr to output character */
int outbytesleft; /* num bytes left in output buffer */
size_t ret_val; /* number of conversions */
/* 初始化转换,获得转换描述符 */
if ((cd = iconv_open(tocode, fromcode)) == (iconv_t)-1)
{
/*error(FATAL, BAD_OPEN);*/
printf("not that code \n" ;
}
inbytesleft = 0; /* no. of bytes converted */
/* translate the characters */
for ( ;; )
{
inchar =str; /* points to input buffer */
outchar =strout; /* points to output buffer */
outbytesleft = BUFSIZ; /* no of bytes to be converted */
if ((bytesread = read(Input, inbuf+inbytesleft,
(size_t)BUFSIZ-inbytesleft)) < 0)
{
perror("prog" ;
return BAD;
printf("read input file error \n" ;
}
if (!(inbytesleft += bytesread))
{
break; /* end of conversions */
}
ret_val = iconv(cd, &inchar, &inbytesleft,
&outchar, &outbytesleft);
if (write(1, outbuf, (size_t)BUFSIZ-outbytesleft) < 0)
{
perror("prog" ;
return BAD;
printf(" write out file error \n" ;
}
if ((ret_val == -1) && (errno == EINVAL))
{
memcpy((char *)inbuf, (char *)inchar,
(size_t)inbytesleft);
printf("error 1 \n" ;
}
else if ((ret_val == -1) && (errno == EILSEQ))
{
/* error(FATAL, BAD_CONVERSION);*/
printf("error 2 \n" ;
}
else if ((ret_val == -1) && (errno == E2BIG))
{
memcpy((char *)inbuf, (char *)inchar,
(size_t)inbytesleft);
(size_t)inbytesleft);
printf("error 3 \n" ;
}
/* Go back and read from the input file. */
/* end conversion & get rid of the conversion table */
/* if (iconv_close(cd) == BAD)
printf(" close error end \n" ;*/
return (0);
}
} |
|