C 读文件

1.纯c读文件

 

int Utf2char(char *src, char** ret)
{
	int nret;
	WCHAR   *pwchar = 0;
	int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
	nret = MultiByteToWideChar(CP_UTF8, 0, src, -1, NULL, 0);
	pwchar = (WCHAR *)malloc(sizeof(WCHAR)*nret);
	nret = MultiByteToWideChar(CP_UTF8, 0, src, -1, pwchar, nret);

	nret = WideCharToMultiByte(codepage, 0, pwchar, -1, 0, 0, 0, 0);
	*ret = (char*)malloc(nret + 1);
	nret = WideCharToMultiByte(codepage, 0, pwchar, -1, *ret, nret, 0, 0);
	free(pwchar);
	return nret;

}

  

 

    
FILE *pFile = NULL;
	pFile = fopen("D:\\codeOneSelf\\****\\Debug\\34.txt", "rb+");
	if (!pFile) {
		return -1;
	}

	fseek(pFile, 0, SEEK_END);
	int len = ftell(pFile);
	fseek(pFile, 0, SEEK_SET);
	char *buf = NULL;
	buf = (char *)malloc(len + 1);
	memset(buf, 0x00, len + 1);

	fread(buf, len, 1, pFile);
	fclose(pFile);
	char *gb2312 = NULL;
	gb2312 = (char *)malloc(BUFFER_LEN * 20);
	memset(gb2312, 0x00, BUFFER_LEN * 20);
	Utf2char(buf, &gb2312);
   free(buf);
   free(gb2312);

  

posted @ 2019-07-26 17:44  木木ing  阅读(219)  评论(0编辑  收藏  举报