I am writing a library in c++ which will be accessed by android/ios/win mobile applications, I have to delete a folder and all its content using c++. I am currently working on c++11 standard. Help in this regard is appreciated.
-
Is [boost](http://www.boost.org/doc/libs/1_64_0/libs/filesystem/doc/reference.html#remove_all) an option? – G.M. May 17 '17 at 10:44
-
Can you use the boost? – voltento May 17 '17 at 10:45
-
Since Android is a platform using boost would require compiling its port manually, I think that it would be easier to split implementation into POSIX (ios, android) and Windows ways and apply the answer to [this question](http://stackoverflow.com/questions/2256945/removing-a-non-empty-directory-programmatically-in-c-or-c) – riodoro1 May 17 '17 at 10:47
-
Your compiler may implement the *filesystem* `Technical Specification` accessible through `#include
`. – Galik May 17 '17 at 11:36
2 Answers
Use boost
it provides platform independence to your application.
If you still want to stick to the conventional way, Macros are the solution. Have a quick glance at code below
#ifdef _WIN32 // note the underscore: without it, it's not msdn official!
// Windows (x64 and x86)
#elif __unix__ // all unices, not all compilers
// Unix
#elif __linux__
// linux
#elif __APPLE__
// Mac OS, not sure if this is covered by __posix__ and/or __unix__
though...
#endif
NOTE: Although the above might work for the basics, remember to verify what macro you want to check for by looking at the Boost.Predef reference pages. directly.

- 807
- 1
- 9
- 26
-
You cannot suggest boost for an app that target mobile, where the filesystem issues are to be handled on platform basis since the apps work inside a sandbox and often on zipped/remote resources. – gabry May 17 '17 at 12:25
-
-
@gabry: The whole point of Boost is to abstract away platform pecularities. Windows uses \ as the native path separator; Boost still allows / etc. Using remote (HTTP) resources is another matter, that's out of scope on _any_ platform. – MSalters May 17 '17 at 12:49
C++11 does not have an API to interact with the filesystem - except to read and write to files. There is no way to delete a file or interact with directories in any way using the standard library.
This will change in C++17, which will introduce the <filesystem>
header.
Until then, you must depend on the platform specific API's. Each platform has their own API for filesystem access - although some conform to the portable POSIX standard. So, you will need to implement your code separately for each different platform. How to implement the deletion in each of the platforms is beyond the scope of my answer, but it'll be described in their respective documentation. There probably also already exists an existing question for each platform on this site.

- 232,697
- 12
- 197
- 326
-
This is a bit too binary. There's not just C++17 `
` but also Boost FileSystem, and the pre-C++17 ` – MSalters May 17 '17 at 12:47`. They're very similar. Depending on the platform API would really be a last resort. -
@MSalters and I'm sure that there are other libraries as well. I'm only concerned with what is guaranteed by the standard. If there is no standard API, then there is platform API. You can always use a library that wraps the platform API. Boost is usually a great choice - not just for file system API. – eerorika May 17 '17 at 12:50