当前位置:网站首页>C language to judge whether a file or folder exists

C language to judge whether a file or folder exists

2022-06-10 22:10:00 m0_ sixty million seven hundred and twenty-one thousand five hu

C Language determines whether a file or folder exists

Collect information collected on the Internet , The following methods are summarized to determine whether a file or folder exists , Note the header file references for each function .

One 、 Use access

  • Function definition

    #include <unistd.h>
    int access(const char * pathname, int mode);

  • Function description

It can be used to determine whether a file or directory exists , access() Will check if it can read / Write an existing file ,access() Only for authority verification , Ignore the form or content of the document , therefore , If a directory is represented as " Writable ", Indicates that you can create new files in this directory , It doesn't mean that this directory can be treated as a file

pathname Is the path to the file or folder , The current directory directly uses the file or folder name ;mode Indicates the inspection mode , common 4 Patterns :

  1. R_OK, 4 Only judge whether you have read permission , This corresponds to the... In the macro definition 00 There is only
  2. W_OK, 2 Only judge whether you have write permission , This corresponds to the... In the macro definition 02 Write permissions
  3. X_OK, 1 Judge whether there is executive authority , This corresponds to the... In the macro definition 04 Read permission
  4. F_OK, 0 Just judge whether it exists , This corresponds to the... In the macro definition 05 Read and write permissions
  • Return value

If the specified access method is valid , Then the function returns 0, no

原网站

版权声明
本文为[m0_ sixty million seven hundred and twenty-one thousand five hu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102050231178.html