0

I can't believe no one seems to have posted this error or the solution. I am spinning up on C++ 17.

I am attempting to run the following code.

fs::directory_entry result(CodeSource::ARDUINO_SOURCE); if (!result.exists()) { fs:create_directory(result); } return result; 

and I get the following result.

terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error' what(): filesystem error: cannot create directory: No such file or directory [~/arduino_source] 

The variable obviously contains "~/arduino_source" Well of course it doesn't exist. That's why I'm creating it.

The docs say it will perform as if I ran mkdir on it and so I tried it and it worked just fine.

I am running Ubuntu 20.10.

3
  • @prehistoricpenguin I don't understand, that's what I'm doing. Commented Apr 15, 2021 at 3:18
  • Could you please provide a minimal-reproducible-example Commented Apr 15, 2021 at 3:22
  • 1
    A '~' is expanded by your shell when you use it on the command line. C++ std::filesystem does not use it and not aware of the existence of something called "home directory". So, it tries to find a directory called "~" in the current directory - and that doesn't exist. Commented Apr 3, 2022 at 16:31

2 Answers 2

3

I have written a minimal program, and confirm that it's caused by the path ~/arduino_source, you need an absolute path or relative path, but for the tilde ~ character, it will cause exceptions.

Since the API directory_entry::directory_entry and std::filesystem::create_directory both can throw, your code needs to handle the exception to make it robust.

Sign up to request clarification or add additional context in comments.

Comments

1

Hi this is a late response but better then never. I was having the same issue and after a little research found that create_directory() only creates a new folder if the parent folder exists. So basically you can create only one new folder at a time. To create multiple new folders, each within the other, use create_directories() [notice the plural "directories"].

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.