码迷,mamicode.com
首页 > 编程语言 > 详细

Linux C语言 检测文件是否存在

时间:2019-11-08 10:44:02      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:win   can   set   com   spec   over   int   div   read   

头文件 unistd.h

if(access(file_name, F_OK ) != -1 ) 
{
    // file exists
} 
else 
{
    // file doesn‘t exist
}

You can also use R_OK, W_OK, and X_OK in place of F_OK to check for read permission, write permission, and execute permission (respectively) rather than existence, and you can OR any of them together (i.e. check for both read and write permission using R_OK|W_OK)

Update: Note that on Windows, you can‘t use W_OK to reliably test for write permission, since the access function does not take DACLs into account. access( fname, W_OK ) may return 0 (success) because the file does not have the read-only attribute set, but you still may not have permission to write to the file.

出处:

https://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c

Graeme Perrow

Linux C语言 检测文件是否存在

标签:win   can   set   com   spec   over   int   div   read   

原文地址:https://www.cnblogs.com/liujx2019/p/11818658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!