0

I want to develop a software which can help me manage the .exe file paths to my games. It is expected to run on Windows.

But unfortunately, my games may have the following 2 format:

  1. ascii + Chinese Character (GBK-encoding), e.g. D:/MyGames/魔女的夜宴/启动.exe
  2. ascii + Japanese Character (shift-JIS encoding), e.g. D:/MyGames/アンレス テルミナリア/エンジン設定.exe

How can I deal with the different encoding problem?

I tried in C++17. It seems that it works on read/write, but I have no idea how to call an exe file.

using namespace std::string_literals;
namespace filesys = std::filesystem;

//using Char = char16_t;
//using String = std::u16string;

using Char = wchar_t;
using String = std::wstring;

void checkExist(const filesys::path& path) {
    std::cout << " existence: " << filesys::exists(path) << '\n';
}

int main () {
    std::setlocale(LC_ALL, "");
    std::locale::global(std::locale(""));

    String str_en(L"../test-english.txt");
    String str_zh(L"../test-中文路径.txt");
    String str_jp(L"../test-日本語パス.txt");

    filesys::path path(str_en);
    filesys::path path_zh(str_zh);
    filesys::path path_jp(str_jp);

    checkExist(path);
    checkExist(path_zh);
    checkExist(path_jp);

/* Output: See below
*/

    filesys::path output_path("read_result.txt");
    std::string s;
    std::ofstream of(output_path);
    getline(std::ifstream(path), s);
    of << s << std::endl;
    getline(std::ifstream(path_zh), s);
    of << s << std::endl;
    getline(std::ifstream(path_jp), s);
    of << s << std::endl;

/* read_result.txt: See below
*/

    wchar_t a[]=L"abc测试测试";
    std::wofstream of("write_output.txt");
    of << a;
/* write_output.txt: See below
*/
}

My file tree

.
├── CMakeLists.txt
├── cmake-build-debug-mingw
├── cmake-build-debug-visual-studio
├── main.cpp
├── test-english.txt
├── test-中文路径.txt
└── test-日本語パス.txt
MinGW compiler MSVC compiler
Output existence: 1
existence: 1
existence: 1
existence: 1
existence: 0
existence: 0
read_result.txt "Eng success"
"Ch success"
"Jap success"
"Eng success"
"Eng success"
"Eng success"
write_output.txt "" "abc测试测试"

Emmm... Maybe I should take think about completing it in Python?

  • 1
    Everything in MS-Windows is UTF-16le. The encoding you are seeing/using is a translation layer on top of UTF-16le, ie just use UTF-16le. – Richard Critten Aug 27 '23 at 15:43
  • IIRC, on Windows the filepaths are `L""` ( `wchar_t[ ]`), not `U""`. And you'd need a `std::wofstream` to store those. – MSalters Aug 28 '23 at 10:52
  • @RichardCritten It becomes... weird that, when I use MSVC as compiler, it cannot even recognive path whether encoded in wchar nor char16_t, but when I use MinGW as compiler, it can. – Moeda Chaos Aug 29 '23 at 08:17
  • @MSalters I have tried to use `std::wcout << L"123测试";`, it can store to a `std::wofstream`-related file, but cannot print to console. (This time only MSVC compiler works). And it is also confusing that, it can write wchar to a file, but cannot read a filepath with wchar – Moeda Chaos Aug 29 '23 at 08:23
  • I then run in python console to find that my console seems to be encoded in CP936. Maybe it is the root of the problems? – Moeda Chaos Aug 29 '23 at 08:40

0 Answers0