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
UTF-8文件读取转换
最新推荐文章于 2025-09-13 10:25:19 发布
本文介绍了一种从UTF-8编码的文件中读取数据并将其转换为ANSI字符串的方法。通过使用C++实现的函数,可以处理文件读取过程中的多种错误情况,并详细展示了如何利用Windows API进行编码转换。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Stable-Diffusion-3.5
图片生成
Stable-Diffusion
Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率
3781

被折叠的 条评论
为什么被折叠?



