是否可以使用apache可移植运行时获取目录中的文件列表?

时间:2013-11-11 13:19:41

标签: apache apr

我需要使用APR获取目录中的文件列表。我该怎么做? 我在文档中寻找答案,但一无所获。 谢谢!

1 个答案:

答案 0 :(得分:2)

您想要的功能是apr_dir_open。我发现头文件是APR的最佳文档

http://apr.apache.org/docs/apr/1.4/group_apr_dir.html

这是一个阅读"的例子。"如果遇到任何问题则报告错误

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <apr.h>
#include <apr_errno.h>
#include <apr_pools.h>
#include <apr_file_info.h>

static void apr_fatal(apr_status_t rv);

int main(void)
{
    apr_pool_t *pool;
    apr_status_t rv;

    // Initialize APR and pool
    apr_initialize();
    if ((rv = apr_pool_create(&pool, NULL)) != APR_SUCCESS) {
        apr_fatal(rv);
    }

    // Open the directory
    apr_dir_t *dir;
    if ((rv = apr_dir_open(&dir, ".", pool)) != APR_SUCCESS) {
        apr_fatal(rv);
    }

    // Read the directory
    apr_finfo_t finfo;
    apr_int32_t wanted = APR_FINFO_NAME | APR_FINFO_SIZE;
    while ((rv = apr_dir_read(&finfo, wanted, dir)) == APR_SUCCESS) {
        printf("%s\t%10"PRIu64"\n", finfo.name, (uint64_t)finfo.size);
    }
    if (!APR_STATUS_IS_ENOENT(rv)) {
        apr_fatal(rv);
    }

    // Clean up
    apr_dir_close(dir);
    apr_pool_destroy(pool);
    apr_terminate();
    return 0;
}

static void apr_fatal(apr_status_t rv)
{
    const int bufsize = 1000;
    char buf[bufsize+1];
    printf("APR Error %d: %s\n", rv, apr_strerror(rv, buf, bufsize));
    exit(1);
}
相关问题