添加中文解决的代码
This commit is contained in:
@@ -1,8 +1,67 @@
|
||||
|
||||
#include "FaceApp.h"
|
||||
#include <windows.h>
|
||||
|
||||
std::wstring UTF8ToWideString(const std::string& utf8str)
|
||||
{
|
||||
if (utf8str.empty()) return L"";
|
||||
|
||||
int wideLen = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(),
|
||||
-1, nullptr, 0);
|
||||
if (wideLen == 0) {
|
||||
throw std::runtime_error("UTF-8转换失败");
|
||||
}
|
||||
|
||||
std::vector<wchar_t> buffer(wideLen);
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), -1,
|
||||
buffer.data(), wideLen);
|
||||
|
||||
return std::wstring(buffer.data());
|
||||
}
|
||||
|
||||
std::wstring ReadUTF8File(const std::string& filePath)
|
||||
{
|
||||
std::ifstream file(filePath, std::ios::binary);
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("无法打开UTF-8文件: " + filePath);
|
||||
}
|
||||
|
||||
// 读取文件所有内容
|
||||
std::string content((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
// 将UTF-8字符串转换为宽字符串
|
||||
return UTF8ToWideString(content);
|
||||
}
|
||||
|
||||
|
||||
std::wstring ReadFileWithChinesePath(const std::wstring& filePath) {
|
||||
// 使用二进制模式打开文件
|
||||
std::ifstream file(filePath, std::ios::binary);
|
||||
if (!file.is_open()) {
|
||||
std::wcout << L"无法打开文件: " << filePath << std::endl;
|
||||
return L"";
|
||||
}
|
||||
|
||||
// 读取文件内容到std::string
|
||||
std::string context;
|
||||
file.seekg(0, std::ios::end);
|
||||
context.resize(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
file.read(&context[0], context.size());
|
||||
file.close();
|
||||
return UTF8ToWideString(context);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
std::string path = "chinese.txt";
|
||||
std::wstring chinese = ReadUTF8File(path);
|
||||
std::wstring chinese_path = UTF8ToWideString("d:/测试目录/中文文件.txt");
|
||||
std::wstring content = ReadFileWithChinesePath(chinese_path);
|
||||
|
||||
InitArg init_arg;
|
||||
Motion motion;
|
||||
motion.type = "bottom_makeup";
|
||||
|
||||
Reference in New Issue
Block a user