'Reading Unicode characters from command line arguments using boost::program_options in Windows
I have several Windows applications that read a file path from command-line arguments. Everything works flawlessly, except when passing paths with non-ANSI characters. I expected this, but don't know how to deal with it. Probably an entry-level question but that is driving me crazy.
My current code looks like:
int main(int argc, char* argv[]) {
namespace po = boost::program_options;
po::options_description po_desc("Allowed options");
po_desc.add_options()
("file", po::value<std::string>(), "path to file");
po::variables_map po_vm;
try {
po::store(po::parse_command_line(argc, argv, po_desc), po_vm);
po::notify(po_vm);
} catch (...) {
std::cout << po_desc << std::endl;
return false;
}
const std::string file_path = po_vm["file"].as<std::string>();
// ...
}
I've found that if I replace the type of file_path
from std::string
to boost::filesystem::path
, some paths are now read. I don't know exactly why but can deduce that it has to be with a translation from the Latin1 charset.
For example, having following files:
malaga.txt
málaga.txt
mąlaga.txt
The first is always read correctly, while the second one fails when using std::string file_path
but not boost::filesystem::path file_path
. The third one always fails.
I've tried switching the main function to int main(int argc, wchar_t* argv)
and using std::wstring
for the argument type, but it is not compatible with boost::program_options
parser.
How can I correctly read such Unicode file names?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|