如何在写入文件之前检查文件是否存在

时间:2014-12-23 00:19:49

标签: c

相当自我解释,在我用C语言写文件之前,如何检查文件是否存在。

1 个答案:

答案 0 :(得分:0)

最喜欢的是access

/* test that file exists (1 success, 0 otherwise) */
int xfile_exists (char *f)
{
    /* if access return is not -1 file exists */
    if (access (f, F_OK ) != -1 )
        return 1;

    return 0;
}

注意: access()由POSIX定义,而不是C,因此其可用性因编译器而异。

相关问题