如何使用apr_file_open()创建文件

时间:2011-06-14 08:19:58

标签: 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 )
    );
}

pathname包含字符串/tmp/tempfile20110614091201

我一直收到错误“权限被拒绝”(结果代码APR_EACCES),但我有权读/写/tmp - 可能导致此问题?

1 个答案:

答案 0 :(得分:3)

我需要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
        APR_FOPEN_WRITE | // open for writing
        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
    );