如何使用apr_file_open()创建一个文件

4
我正在调用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
我创建了这个问题和答案,因为我在互联网上搜索了一段时间,但找不到答案。 - PP.
你好。请问您能告诉我如何在此步骤之后将HTTP请求的参数及其值附加到文件中吗? - weefwefwqg3
1个回答

5

我需要使用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
    );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接