简体   繁体   English

无法删除C ++中的空文件夹

[英]Can't remove an empty folder in C++

I want to remove an empty folder using remove() in C++ on Windows 7 but I can't. 我想在Windows 7上的C ++中使用remove()删除一个空文件夹,但不能。 I tried rmdir() instead of remove() then the folder got removed! 我尝试使用rmdir()而不是remove()然后remove()了文件夹! Nevertheless, the reason why I don't use rmdir() is due to Android. 但是,我不使用rmdir()的原因是由于Android。 In a library project for Android, I can't include "direct.h" header so can't use rmdir() , either. 在Android的库项目中,我不能包含“ direct.h”标头,因此也不能使用rmdir() Unlike on Windows, the function remove() works well on Android. 与Windows不同,函数remove()在Android上运行良好。 I don't understand why. 我不明白为什么。

Anybody knows why this is happening? 有人知道为什么会这样吗?

Or any other functions which will work on both Windows and Android? 或者在Windows和Android上都可以使用的其他任何功能?

This is a pretty common problem when writing cross-platform programs. 在编写跨平台程序时,这是一个非常普遍的问题。

Sometimes, a library can provide the abstraction you need. 有时,库可以提供您所需的抽象。 For example, Boost has a filesystem library that can enumerate files, manipulate directories, etc., on multiple platforms using the exact same code. 例如, Boost具有一个文件系统库,该库可以使用完全相同的代码在多个平台上枚举文件,操纵目录等。

Also, there are usually symbols defined that allow you to detect which compiler is currently building your code. 另外,通常会定义一些符号,这些符号使您能够检测当前正在构建代码的编译器。 Even if there isn't one that does what you want, you can define your own. 即使没有人可以做您想要的,您也可以定义自己的。

Let's say you need to build your software for two different fictitional operating systems named FooOS and for BarOS. 假设您需要为名为FooOS和BarOS的两​​个不同的虚拟操作系统构建软件。 I'm going to invent two symbols, FOO_OS and BAR_OS . 我将发明两个符号FOO_OSBAR_OS In my code, I can do something like this: 在我的代码中,我可以执行以下操作:

#ifdef FOO_OS
#include <foo_stuff.h>
#elseif BAR_OS
#include <bar_stuff.h>
#endif

void do_something()
{
#ifdef FOO_OS
    do_it_this_way();
#elseif BAR_OS
    do_it_that_way();
#endif
}

Now, we just need to either define FOO_OS or BAR_OS . 现在,我们只需要定义FOO_OSBAR_OS This can be done through an IDE's project configuration or on the command line of the compiler. 这可以通过IDE的项目配置或在编译器的命令行上完成。 Use Google to find out about your particular situation, since you didn't include those details in your post. 由于您没有在帖子中添加这些详细信息,因此请使用Google来查找您的具体情况。

There is a preprocessing step when you compile your code that makes a pass through the source, and applies these conditional statements. 编译代码时,有一个预处理步骤,该步骤将遍历源代码并应用这些条件语句。 A following pass actually compiles the code. 接下来的过程实际上是编译代码。 Here is some documentation about Visual Studio's preprocessor, for example. 例如,这是有关Visual Studio预处理器的一些文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM