Viktor Danov Viktor Danov - 1 year ago 66
C++ Question

Renaming a file with an en dash in the name in C++

In the project I'm working on, I work with files and I check if they exists before proceeding. Renaming or even working with files featuring that 'en dash' in the file path seems impossible.

std::string _old = "D:\\Folder\\This – by ABC.txt";
std::rename(_old.c_str(), "New.txt");

here the _old variable is interpreted as D:\Folder\This û by ABC.txt
I tried

setlocale(LC_ALL, "");
setlocale(LC_ALL, "C");
setlocale(LC_ALL, "en_US.UTF-8");

but none of them worked.. What should be done?

Answer Source

It really platform dependant, Unicode is headache. Depends on which compiler you use. For older ones from MS (VS2010 or older), you would need use API described in MSDN

for VS2015

std::string _old = u8"D:\\Folder\\This \xe2\x80\x93 by ABC.txt"s;

according to their docs. I can't check that one.

for mingw, gcc, etc.

std::string _old = u8"D:\\Folder\\This \xe2\x80\x93 by ABC.txt";
std::cout <<;

output contains proper file name...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download