zoukankan      html  css  js  c++  java
  • 测试文件是否可读、存在等

     1 #include <unistd.h>
    2 #include <sys/stat.h>
    3 #include <stdio.h>
    4
    5 bool IsFileRead(const char * file_name)
    6 {
    7 int ret = access(file_name, R_OK);
    8 /*
    9 W_OK可写 X_OK可执行 F_OK是否存在
    10 */
    11 if (ret == 0)
    12 return true;
    13 return false;
    14 }
    15 bool IsRegFile(const char * file_name)
    16 {
    17 struct stat s;
    18 if (lstat(file_name, &s) >= 0 && S_ISREG(s.st_mode)) {
    19 /*
    20 S_ISDIR S_ISCHR字符设备 S_ISBLK S_ISFIFO S_ISLNK S_ISSOCK
    21 */
    22 return true;
    23 }
    24 return false;
    25
    26 /*if (lstat(file_name, &s) < 0) { // 文件不存在会返回负数
    27 printf("error\n");
    28 return false;
    29 }
    30 if (S_ISREG(s.st_mode)) {
    31 return true;
    32 }
    33 return false;*/
    34 }
    35
    36 int main(int argc, char **argv)
    37 {
    38 if (IsRegFile(argv[1])) {
    39 printf("exist file: %s\n", argv[1]);
    40 } else {
    41 printf("not exist file: %s\n", argv[1]);
    42 }
    43
    44 return 0;
    45 }
  • 相关阅读:
    1_Selenium环境搭建
    python functools
    python 参数注解inspect
    python 堆排序
    python functools
    python 装饰器
    python 柯里化
    python 高阶函数
    python 树
    python 函数销毁
  • 原文地址:https://www.cnblogs.com/tzhangofseu/p/2268677.html
Copyright © 2011-2022 走看看