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 }
posted on 2011-11-30 10:14  吃吃户  阅读(251)  评论(0编辑  收藏  举报