用于目录访问的 C 库

发布于 2024-07-26 07:53:30 字数 682 浏览 2 评论 0原文

我知道标准 C 无法让我对文件夹执行任何操作,但我想要一种相当可移植且跨平台的方式来访问文件夹。 这时,我需要做的就是创建一个文件夹,检查一个文件夹是否存在,可能的话删除一个文件夹。 我可以预见在不久的将来需要从文件夹中读取文件,但这并不是一个非常紧迫的需求。

不管怎样,我想知道是否有一个好的跨平台 C 库来处理目录。 在绝对紧要关头,我可能可以自己在 POSIX 和 Windows 上工作,但我想知道是否已经有任何好的东西了。 我一直在考虑使用 GLib 或 Apache Portable Runtime,但这两者都提供了比我真正需要的更多的东西,而且我希望保持其相当轻量级。 我还考虑过使用流行脚本语言的内部结构,例如 Perl 或 Python,但这对于目录函数来说似乎也有很大的开销。

如果有人有什么需要添加到我应该研究的列表中,或者想要为我已经列出的选项之一提供一个很好的案例,请告诉我。 我不想听起来像是在要求代码,但是如果您发布了一个简单的函数,例如 int direxist(char *dirname) ,如果目录存在则返回 true ,否则返回 false ,只是为了说明您选择的库的 API,这真的很棒,而且我想不会太难。 如果你想提倡使用 POSIX/滚动我自己的,也这样做,因为我很喜欢通过自己做来学习这样的新东西。

只是为了确定一下,我想要 C,而不是 C++。 我确信 boost 很好,但我对 C++ 解决方案不感兴趣。

I know that standard C doesn't give me any ability to do anything with folders, but I would like a fairly portable and cross-platform way to access folders. At this time, all I need to do is make a folder, check if a folder exists, and possibly delete a folder. I can forsee needing to read files from a folder in the near future, but that's not a very pressing need.

Anyway, I was wondering if there was a good cross-platform C library for working with directories. In an absolute pinch I can probably roll my own to work on POSIX and Windows, but I was wondering if there were any good ones already out there. I've been considering GLib or the Apache Portable Runtime, but both of those come with a lot more stuff than I really need, and I'd like to keep this fairly lightweight. I've also considered using the internals of a popular scripting language, like Perl or Python, but that also seems like a lot of overhead just for directory functions.

If anyone has anything to add to this list that I should look into, or wants to make a good case for one of the options I've already listed, please tell me. I don't want to sound like I'm asking for code, but if you posted a simple function like int direxist(char *dirname) that returned true if the directory exists and false otherwise, just to illustrate the API for your library of choice, that would be really awesome, and I imagine not too hard. If you want to advocate using POSIX/rolling my own, do that too, because I'm a sucker for learning new stuff like this by doing it myself.

Just to make sure, I want C, not C++. I'm sure boost is good, but I'm not interested in C++ solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2024-08-02 07:53:30

我会加入 APR 的行列。 它确实为您提供了比目录访问更多的功能,但它是我使用过的最好的多平台 C 库。 无论如何,您很可能会发现自己将来需要它的一些其他组件,因此您不妨将它们放在手边。

另一种选择是通过 Win32 实现 POSIX API 集,并用 POSIX 编写所有内容。 这里的好处是 Windows 很快成为唯一不包含 POSIX 运行时实现的现代操作系统。

I would jump on the APR bandwagon. It does give you a lot more than directory access, but it is the best multi-platform C library that I've used. Chances are that you will find yourself needing some of the other components of it in the future anyway, so you might as well have them handy.

The other option is to implement the POSIX API set over Win32 and just write everything in POSIX. The bonus here is that the Windows is quickly becoming the only modern OS that does not include a POSIX runtime implementation.

浸婚纱 2024-08-02 07:53:30

我一直在考虑 GLib 或 Apache Portable Runtime,但这两者都提供了比我真正需要的更多的东西,而且我希望保持它相当轻量。

GLib 很可能已经安装(至少在 GNU/Linux 上)。 唯一的问题是它会添加依赖项。

我还考虑过使用流行脚本语言的内部结构,例如 Perl 或 Python,但这对于目录功能来说似乎也有很大的开销。

我宁愿首先使用 Python,也可能使用 C 来处理代码的特定部分。

>>> def direxist(dirname):
...     return os.path.isdir(dirname)
... 
>>> direxist('/')
True
>>> direxist('/home')
True
>>> direxist('/home/bastien/Petites leçons de typographie.pdf')
False

关于编写您自己的 C 函数,它会是这样的:

#include <stdio.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

int direxist(const char* dirname)
{
#ifdef _WIN32
    /* ... */
#else
    struct stat fileinfo;
    int ret = -1;

    if (stat(dirname, &fileinfo) == -1)
    {
        perror("direxist");
    }
    else
    {
        if (S_ISDIR(fileinfo.st_mode))
        {
            ret = 1;
        }
        else
        {
            ret = 0;
        }
    }

    return ret;
#endif
}

int
main (void)
{
    printf("%d\n", direxist("/"));

    return 0;
}

我不知道如何使用 Win32 来实现它,所以您需要自己找到它。

但是,我强烈建议使用外部库。 仅使用 C 库或重新发明轮子并不能走得太远。

I've been considering GLib or the Apache Portable Runtime, but both of those come with a lot more stuff than I really need, and I'd like to keep this fairly lightweight.

It's quite probable that GLib will already be installed (on GNU/Linux, at least). The only problem is that it will add a dependency.

I've also considered using the internals of a popular scripting language, like Perl or Python, but that also seems like a lot of overhead just for directory functions.

I would rather use Python in the first place, and possibly use C for specific parts of code.

>>> def direxist(dirname):
...     return os.path.isdir(dirname)
... 
>>> direxist('/')
True
>>> direxist('/home')
True
>>> direxist('/home/bastien/Petites leçons de typographie.pdf')
False

About writing your own C function, it would go something like this:

#include <stdio.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

int direxist(const char* dirname)
{
#ifdef _WIN32
    /* ... */
#else
    struct stat fileinfo;
    int ret = -1;

    if (stat(dirname, &fileinfo) == -1)
    {
        perror("direxist");
    }
    else
    {
        if (S_ISDIR(fileinfo.st_mode))
        {
            ret = 1;
        }
        else
        {
            ret = 0;
        }
    }

    return ret;
#endif
}

int
main (void)
{
    printf("%d\n", direxist("/"));

    return 0;
}

I don't how to do it with Win32, so you'll to find that yourself.

However, I would strongly recommend using an external library. You don't go far with just the C library or by reinventing the wheel.

泪意 2024-08-02 07:53:30

我认为你应该使用 APR 或类似的东西。 根据我的经验,设计一个 POSIX API 然后在 Windows 上实现它效果并不好(我必须承认这是相当有限的)。

文件 IO 和相关语义差异太大,因此您必须立即设计 API 来处理窗口。 对于可以放入公共 API 中的内容也存在不同的限制。 此类问题的一个例子是用于文件处理的 python C API。 它显然是用 POSIX POV 设计的,并且由于共享 C 运行时对象等原因,在 Windows 上使用它非常困难。

I think you should use APR or something in the same vein. Designing a POSIX API and then implement it for windows does not work well in my experience (which is quite limited I must confess).

File IO, and related semantics are just too different, so you have to design your API to deal with windows right away. There are also different constraints on what can be put in the public API. One example of such a problem is the python C API for file handling. It was clearly designed with a POSIX POV, and it is very difficult to use it on windows because of things like sharing C runtimes objects, etc...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文