To simplify the problem I'm trying to solve, let's say I'm trying to build an CLI that check if a path exists using std::filesystem::exists(path)
. This path
came from the user input.
Here's two constraint:
- I'm developing on Windows.
- I cannot use
wmain
because I don't have access to the main function (imagine the CLI is a third-party software and I'm writing a plugin to it). - The argc and argv will be passed to my function with the exact signature below (see the snippet code).
Here's an example code that I write:
#include <iostream>
#include <filesystem>
void my_func(int argc, char *argv[]) {
// This is the only place I can do my works...
if (argc > 1)
{
bool is_exist = std::filesystem::exists(argv[1]);
std::cout << "The path (" << argv[1] << ") existence is: " << is_exist << std::endl;
}
else
{
std::cout << "No path defined" << std::endl;
}
}
int main(int argc, char *argv[]) {
my_func(argc, argv);
return 1;
}
The user can use the software with the following command:
./a.exe path/to/my/folder/狗猫
Currently, rubbish is being printed on the terminal. But from my research, this is not C++ problem, but rather a cmd.exe problem.
And if it is not already obvious, the above code snippet does not work even though there is a folder called 狗猫.
My guess is I have to manually convert char[] to filesystem::path somehow. Any help is greatly appreciated.