问题描述
我试图在Windows上使用汉字创建一个文件。整个路径在变量std :: string originalPath内,但是,我有一个字符集问题,我根本无法理解克服。
I'm trying to create a file on Windows using a Chinese character. The entire path is inside the variable "std::string originalPath", however, I have a charset problem that I simply cannot understand to overcome.
我写了以下代码:
#include <iostream>
#include <boost/locale.hpp>
#include <boost/filesystem/fstream.hpp>
#include <windows.h>
int main( int argc, char *argv[] )
{
// Start the rand
srand( time( NULL ) );
// Create and install global locale
std::locale::global( boost::locale::generator().generate( "" ) );
// Make boost.filesystem use it
boost::filesystem::path::imbue( std::locale() );
// Check if set to utf-8
if( std::use_facet<boost::locale::info>( std::locale() ).encoding() != "utf-8" ){
std::cerr << "Wrong encoding" << std::endl;
return -1;
}
std::string originalPath = "C:/test/s/一.png";
// Convert to wstring (**WRONG!**)
std::wstring newPath( originalPath.begin(), originalPath.end() );
LPWSTR lp=(LPWSTR )newPath.c_str();
CreateFileW(lp,GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |
FILE_SHARE_WRITE, NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL );
return 0;
}
运行它,但是, :\test\s一个名为¦ㅈタ.png的文件,而不是一.png,我想要的。我发现克服这一点的唯一方法是交换线路。
Running it, however, I get inside the folder "C:\test\s" a file of name "¦ᄌタ.png", instead of "一.png", which I want. The only way I found to overcome this is to exchange the lines
std::string originalPath = "C:/test/s/一.png";
// Convert to wstring (**WRONG!**)
std::wstring newPath( originalPath.begin(), originalPath.end() );
简单
std::wstring newPath = L"C:/test/s/一.png";
在这种情况下,文件一.png完全出现在文件夹C:\test \s。尽管如此,我不能这样做,因为软件从std :: string变量获取它的路径。我认为从std :: string到std :: wstring的转换正在执行错误的方式,但是,正如可以看到,我有深刻的问题,试图理解这个逻辑。我阅读并彻底研究了谷歌,阅读了许多定性文本,但我的所有尝试似乎都没有用。我尝试了MultiByteToWideChar函数和boost :: filesystem,但是两者都没有帮助,我根本无法在写入文件夹时获得正确的文件名。
In this case the file "一.png" appears perfectly inside the folder "C:\test\s". Nonetheless, I cannot do that because the software get its path from a std::string variable. I think the conversion from std::string to std::wstring is being performed the wrong way, however, as it can be seen, I'm having deep problem trying to understand this logic. I read and researched Google exhaustively, read many qualitative texts, but all my attempts seem to be useless. I tried the MultiByteToWideChar function and also boost::filesystem, but both for no help, I simply cannot get the right filename when written to the folder.
学习,所以我很抱歉,如果我做一个愚蠢的错误。我的IDE是Eclipse,它设置为UTF-8。
I'm still learning, so I'm very sorry if I'm making a dumb mistake. My IDE is Eclipse and it is set up to UTF-8.
推荐答案
你需要实际转换UTF- UTF-16。为此,你必须查找如何使用 boost :: locale :: conv
或者(在Windows上) MultiByteToWideChar
function。
You need to actually convert the UTF-8 string to UTF-16. For that you have to look up how to use boost::locale::conv
or (on Windows only) the MultiByteToWideChar
function.
std :: wstring newPath(originalPath.begin(),originalPath.end());
将不会工作,它将简单地复制所有字节逐个并将它们转换为wchar_t。
std::wstring newPath( originalPath.begin(), originalPath.end() );
won't work, it will simply copy all the bytes one by one and cast them to a wchar_t.
这篇关于使用中文字符时文件名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!