免费注册 查看新帖 |

Chinaunix

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

码制转换问题 [复制链接]

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

论坛徽章:
0
2 [报告]
发表于 2005-05-23 11:49 |只看该作者

码制转换问题

把编译信息贴上来啊。

论坛徽章:
0
3 [报告]
发表于 2005-06-04 16:42 |只看该作者

码制转换问题

\"函数中的Input---- 指iint 型的nput file descriptor,输入文件描述符\"--就是文件句柄,open(fopen)打开文件的返回值
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP