bool ReadUtf8ToString(const string &filename, string &content){
content = "";
FILE *myfile;
wchar_t *name= new wchar_t[filename.length() * 2 + 5];
if(name == NULL){
cerr << "ReadUtf8ToString: Memory allocate error for " << filename << endl;
return false;
}
swprintf(name, L"%S", filename.c_str());
//ANSI string => Uicode string,注意,S要大写
myfile=_wfopen(name,L"rb"); //以二进制方式读
if(myfile == NULL){
cerr << "ReadUtf8ToString: Can't open file: " << filename << endl;
return false;
}
fseek(myfile,3,0);
char buff[4098];
//取得所有UTF-8字节
while(fgets(buff,4098,myfile)){
//先把UTF-8转换为UNICODE
int num=MultiByteToWideChar(CP_UTF8,0,buff,-1,NULL,0);
if(num <= 0){
cerr << "UTF-8 Converting to UNICODE error for " << filename << endl;
return false;
}
wchar_t *buffw2=new wchar_t[num];
if(buffw2 == NULL){
cerr << "ReadUtf8ToString: Memory allocate error for " << filename << endl;
return false;
}
MultiByteToWideChar(CP_UTF8,0,buff,-1,buffw2,num);
//再将Unicode转换为ANSI字符串
int len = WideCharToMultiByte(CP_ACP, 0, buffw2, num, NULL, 0, NULL, NULL);
if(len <= 0){
cerr << "UNICODE Converting to ANSIC error for " << filename << endl;
return false;
}
LPSTR lpsz = new CHAR[len+1];
if(lpsz == NULL){
cerr << "ReadUtf8ToString: Memory allocate error for " << filename << endl;
return false;
}
WideCharToMultiByte(CP_ACP, 0, buffw2, num, lpsz, len, NULL, NULL);
lpsz[len]='\0';
content += string(lpsz);
delete []buffw2;
delete []lpsz;
}
delete []name;
return true;
}
ReadUtf8ToString
最新推荐文章于 2024-04-18 22:21:21 发布