c - 如何使用 apr_file_open() 创建文件

标签 c apr apache-portable-runtime

我正在对 Apache Portable Runtime 库(版本 1.4)进行以下调用:

result = apr_file_open(
    &file, // new file handle
    pathname, // file name          
    APR_FOPEN_CREATE | // create file if not there 
    APR_FOPEN_EXCL | // error if file was there already
    APR_FOPEN_APPEND | // move to end of file on open
    APR_FOPEN_BINARY | // binary mode (ignored on UNIX)
    APR_FOPEN_XTHREAD | // allow multiple threads to use file
    0, // flags
    APR_OS_DEFAULT |
    0, // permissions
    pool // memory pool to use
);

if ( APR_SUCCESS != result ) {
    fprintf(
        stderr,
        "could not create file \"%s\": %s",
        pathname,
        apr_errorstr( result )
    );
}

并且路径名包含字符串/tmp/tempfile20110614091201

我不断收到错误“权限被拒绝”(结果代码 APR_EACCES),但我有权读取/写入 /tmp - 可能是什么原因导致的?

最佳答案

我需要 APR_FOPEN_WRITE 标志。我错误地认为 APR_FOPEN_APPEND 标志就足够了。

因此,有效的调用是:


    result = apr_file_open(
        &file, // new file handle
        pathname, // file name          
        APR_FOPEN_CREATE | // create file if not there 
        APR_FOPEN_EXCL | // error if file was there already
        <b>APR_FOPEN_WRITE | // open for writing</b>
        APR_FOPEN_APPEND | // move to end of file on open
        APR_FOPEN_BINARY | // binary mode (ignored on UNIX)
        APR_FOPEN_XTHREAD | // allow multiple threads to use file
        0, // flags
        APR_OS_DEFAULT |
        0, // permissions
        pool // memory pool to use
    );

关于c - 如何使用 apr_file_open() 创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6340865/

相关文章:

C语言 sizeof(char[]) 获取静态大小

c - 为什么在 C 中使用指针时不需要指定数组大小?

c - 为什么它会随机读取文件?

编译简单的 Apache Portable Runtime 1.5.2 程序

c - 如何编译 APR 测试脚本

c# - DllImport 在 Windows XP SP3 上失败,但在 Windows 7 上有效

ssl - 当我使用 APR 并且并非所有 Web 部署都使用 CLIENT-CERT 身份验证时,如何在 JBoss 5.1.0.GA 中获得客户端证书身份验证?

eclipse - 在 Spring Tool Suite/Eclipse 中配置 APR 连接器

c++ - 如何休眠 APR 线程?

c++ - APR(Apache可移植运行时)1.2.2 RegEx?