I have the following code.
What it does is from a relative Path it should remove the filename.
My expected result would be ".\png" what I get is an empty string...
#include <iostream>
#include <filesystem>
#include <string>
//using namespace std;
namespace fs = std::filesystem;
#define VNAME(xx) (#xx)
int main() {
std::cout << "Hello World!" << "\n";
std::string fpath = ".\\png\\DUMMY-AB-42-L.PNG";
auto fs_fpath = fs::path(fpath);
auto fs_fpath_str = fs_fpath.string();
std::cout << VNAME(fpath) << ":=" << fpath << "\n";
std::cout << VNAME(fs_fpath_str) << ":=" << fs_fpath_str << "\n";
fs_fpath.remove_filename();
fs_fpath_str = fs_fpath.string();
std::cout << VNAME(fs_fpath_str) << ":=" << fs_fpath_str << "\n";
return 0;
}
the output is:
Hello World!
fpath:=.\png\DUMMY-AB-42-L.PNG
fs_fpath_str:=.\png\DUMMY-AB-42-L.PNG
fs_fpath_str:=
The simple Question is: Bug or Feature ?
I have the following code.
What it does is from a relative Path it should remove the filename.
My expected result would be ".\png" what I get is an empty string...
#include <iostream>
#include <filesystem>
#include <string>
//using namespace std;
namespace fs = std::filesystem;
#define VNAME(xx) (#xx)
int main() {
std::cout << "Hello World!" << "\n";
std::string fpath = ".\\png\\DUMMY-AB-42-L.PNG";
auto fs_fpath = fs::path(fpath);
auto fs_fpath_str = fs_fpath.string();
std::cout << VNAME(fpath) << ":=" << fpath << "\n";
std::cout << VNAME(fs_fpath_str) << ":=" << fs_fpath_str << "\n";
fs_fpath.remove_filename();
fs_fpath_str = fs_fpath.string();
std::cout << VNAME(fs_fpath_str) << ":=" << fs_fpath_str << "\n";
return 0;
}
the output is:
Hello World!
fpath:=.\png\DUMMY-AB-42-L.PNG
fs_fpath_str:=.\png\DUMMY-AB-42-L.PNG
fs_fpath_str:=
The simple Question is: Bug or Feature ?
Share Improve this question edited Feb 15 at 0:40 schnedan asked Feb 14 at 14:11 schnedanschnedan 2822 silver badges11 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 7Backslash ( \
) characters are valid in directory and file names on Unix.
So, on Unix, ".\\png\\DUMMY-AB-42-L.PNG"
is a just a filename, without directory.
With "./png/DUMMY-AB-42-L.PNG"
, you will have expected output (on both Unix and windows).
Demo
or use
auto fs_fpath = fs::path(".") / "png" / "DUMMY-AB-42-L.PNG";
fs_fpath.filename()
(before trying to remove the file name, since this is what is supposed to be removed)? Maybe the issue is not really the removal. – JaMiT Commented Feb 14 at 14:19\\
by/
resolve the issue Demo – Jarod42 Commented Feb 14 at 14:22\
) characters are valid in directory and file names on Unix – Jarod42 Commented Feb 14 at 14:26