Test whether a file is readable and exists.

Source: Internet
Author: User
Tags lstat
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: can write X_ OK? can execute 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 character device S_ISBLK S_ISFIFO S_ISLNK S_ISSOCK
21 */
22 return true;
23}
24 return false;
25
26/* if (lstat (file_name, & s) <0) {// if the file does not exist, a negative number is returned.
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}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.