【问题标题】:Is it possible to get list of files in directory using apache portable runtime?是否可以使用 apache 便携式运行时获取目录中的文件列表?
【发布时间】:2013-11-23 07:36:44
【问题描述】:

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

【问题讨论】:

    标签: apache apr


    【解决方案1】:

    你想要的函数是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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2022-10-30
      • 2021-12-24
      • 2011-03-27
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多