我想保存一个带有名称的文件Привет Мир.jpg
我收到一个字符串(例如从文件中读取)(其中包含 unicode),但我的 C++ 代码将其保存为ÐÑÐ¸Ð²ÐµÑ ÐиÑ.jpg
我该怎么做才能正确保存它? (顺便说一句,如果我只是将该字符串保存到文件中,它会正确保存,这意味着只有我保存文件名的方式是错误的。如何解决这个问题?)
这是我的文件保存代码:
void file_service::save_string_into_file( std::string contents, std::string name )
{
std::string pathToUsers = this->root_path.string() + "/users/";
boost::filesystem::path users_path ( this->root_path / "users/" );
users_directory_path = users_path;
general_util->create_directory(users_directory_path);
std::ofstream datFile;
name = users_directory_path.string() + name;
datFile.open(name.c_str(), std::ofstream::binary | std::ofstream::trunc | std::ofstream::out );
datFile.write(contents.c_str(), contents.length());
datFile.close();
}
where
void general_utils::create_directory( boost::filesystem::path path )
{
if (boost::filesystem::exists( path ))
{
return;
}
else
{
boost::system::error_code returnedError;
boost::filesystem::create_directories( path, returnedError );
if ( returnedError )
{
throw std::runtime_error("problem creating directory");
}
}
}
Update: 在帮助下 https://stackoverflow.com/questions/6975267/c-saving-file-with-unicode-name-problem-how-to-save-utf-8-filenames-correctly/6975299#6975299我现在有
void file_service::save_string_into_file( std::string contents, std::string s_name )
{
boost::filesystem::path users_path ( this->root_path / "users" );
users_directory_path = users_path;
general_util->create_directory(users_directory_path);
boost::filesystem::ofstream datFile;
boost::filesystem::path name (users_directory_path / s_name);
datFile.open(name, std::ofstream::binary | std::ofstream::trunc | std::ofstream::out );
datFile.write(contents.c_str(), contents.length());
datFile.close();
}
但是当我保存文件时,它会将其文件名保存为Привет РњРёСЂ.jpg
所以..我现在该怎么办?