最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - std::filesystem rel. path + remove_filename() results in empty string - Bug or feature? - Stack Overflow

programmeradmin2浏览0评论

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
  • 2 Your output doesn't match your code. Could you make sure you are running the same code that you posted? – Nate Eldredge Commented Feb 14 at 14:18
  • What is returned by 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
  • 2 Changing \\ by / resolve the issue Demo – Jarod42 Commented Feb 14 at 14:22
  • Backslash ( \ ) characters are valid in directory and file names on Unix – Jarod42 Commented Feb 14 at 14:26
  • 5 By the way, try to reduce your code to the (minimal reproducible part of the) issue only and remove all the unnecessary stuff around. It should have been no more than this: godbolt./z/hdG93P8rh – Fareanor Commented Feb 14 at 14:26
 |  Show 3 more comments

1 Answer 1

Reset to default 7

Backslash ( \ ) 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";
发布评论

评论列表(0)

  1. 暂无评论