69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>
typedef unsigned short wchar;
main(){
size_t n;
FILE *stream = NULL;
wchar szBuffer[1024];
int i;
setlocale(LC_ALL,"");
stream = fopen("test.txt","rb");
if (stream == NULL)
{
return -1;
}
while((n=fread(szBuffer,2,1024,stream)) > 0)
for(i=0; i<n; i++){
putwchar(szBuffer[i]);
}
}
FILE *stream = NULL;
stream = fopen("/var/www/vhosts/hzy13207.cn/httpdocs/hzy1.txt","r+b");
if (stream == NULL)
{
printf("open file failed");
return -1;
}
string str;
char szBuffer[1025] = {0};
char szTemp[3073] = {0};
size_t inBytes = 1024;
size_t outBytes = 3072;
while(fgets(szBuffer,1024,stream) != NULL)
{
printf("%s\n",szBuffer);
iconv_t conv;
conv = iconv_open("UTF-8", "UTF-16"); //WCHAR_T即unicode
//iconv函数会产生段错误 这个是什么原因
if(iconv(conv, (char **)&szBuffer, &inBytes, (char **) &szTemp, &outBytes)==-1)
{
switch(errno)
{
case E2BIG:
printf("E2BiG\n");
break;
case EILSEQ:
printf("EILSEQ\n");
break;
case EINVAL:
printf("EINVAL\n");
break;
}
}
iconv_close(conv);
//string strTemp(szTemp);
//str += strTemp;
printf("unicode\n");
memset(szBuffer,0,1025);
memset(szTemp,0,3073);
}
//printf("con:%s\n",str.c_str());
fclose(stream);