I'm developing a Win32 application that has a configuration dialog where the user can specify file paths to things like the database root / working directory. Up until now, in testing, I've been using this functionality with relative file paths, as my file structure is not spread out too much. Also, my config.txt file, which is in the same directory as the .exe file, is loaded on startup, calling the std::ifstream constructor with a relative, hard-coded path.
As I'm newly working with the command line (allowing to drag files on the program's .exe file, therefore passing their paths), the path to the config.txt file somehow is not recognized as valid
and my program (accordingly) throws an error upon this. Replacing the relative, hard-coded path with an absolute path won't fix this, because then, there's the problems with white spaces, backslashes (possibly German Umlaute in my user name too...).
Besides the problem with the config.txt path, I now have attempted to paste absolute paths from the address bar of Windows Explorer
into the edit controls of my program's configuration dialog. However with these paths, I run into pretty much the same problems.
To round this up (and possibly further hint to the source of my problem), the same behaviour occurrs when I drag files on the .exe
to pass their paths. Luckily, the paths (containing whitespaces) are quoted in the command line, so separation is easy, but after reading in, they behave just like the config.txt path and paths entered into the configuration dialog.
Long story short - I am looking for a way to convert paths from the command line or the address bar of Windows Explorer into valid arguments to the std::ifstream constructor. The issues dealt with here are common actually - the standard whitespaces, backslash and Umlaute (Unicode) things.
Here's a minimal example of reading in an absolute path from the dialog and trying to open a std::ifstream:
char path[256];
HWND hEditCtrl = GetDlgItem(hwndConfigurationDlg, IDC_CONFIG_EDITTEXT_DATABASE);
GetWindowText(hEditCtrl, path, sizeof(path));
// path contains something like
// "C:\Users\Firstname Lastname\Documents\2__Programming\Desktop\Gerät.txt"
// notice the whitespace, backslashes, German Umlaut.
std::ifstream ifstr(path, ios::in);
if (!ifstr.good()){
MessageBox(NULL, "The file was not found at the specified path.",
"File Open Error", MB_OK);
}