C language to judge whether the file exists stat, fopen, access

One, stat

Header files sys/stat.h unistd.h
function prototype

Structure struct stat description

struct stat {<!-- -->
    dev_t st_dev; //device file device number
    ino_t st_ino; //i-node of the inode file
    mode_t st_mode; //protection file type and access permissions
    nlink_t st_nlink; //number of hard links The number of hard links connected to the file, the value of the newly created file is 1.
    uid_t st_uid; //user ID of owner User ID of the owner of the file
    gid_t st_gid; //group ID of owner The group ID of the owner of the file
    dev_t st_rdev; //device type If this file is a device file, its device number
    off_t st_size; //total size, in bytes file size, calculated in bytes
    unsigned long st_blksize; //blocksize for filesystem I/O The I/O buffer size of the file system.
    u nsigned long st_blocks; //number of blocks allocated occupies the number of file blocks, each block size is 512 bytes.
    time_t st_atime; //time of lastaccess The last access or execution time of the file, generally only changed when using mknod, utime, read, write and tructate.
    time_t st_mtime; //time of last modification The time when the file was last modified, generally only changed when using mknod, utime and write
    time_t st_ctime; //time of last change i-node last changed time, this parameter will be updated when the file owner, group, permissions are changed
};
1. S_IFMT 0170000 bitmask for file type
2. S_IFSOCK 0140000 socket
3. S_IFLNK 0120000 symbolic link
4. S_IFREG 0100000 General file
5. S_IFBLK 0060000 block device
6. S_IFDIR 0040000 directory
7. S_IFCHR 0020000 character device
8. S_IFIFO 0010000 First in first out
9. S_ISUID 04000 (set user-id on execution) bit of the file
10. S_ISGID 02000 (set group-id on execution) bit of the file
11. The sticky bit of the S_ISVTX 01000 file
12. S_IRUSR (S_IREAD) 00400 The file owner has read permission
13. S_IWUSR (S_IWRITE) 00200 The file owner has write permission
14. S_IXUSR (S_IEXEC) 00100 The file owner has executable permission
15. S_IRGRP 00040 user group has read permission
16. S_IWGRP 00020 user group has write permission
17. S_IXGRP 00010 user group has executable permission
18. S_IROTH 00004 Other users have read permission
19. S_IWOTH 00002 Other users have write permission
20. S_IXOTH 00001 Other users have executable permissions The above file types are defined in POSIX to check the macro definitions of these types
21. Whether S_ISLNK (st_mode) is a connection
22. Is S_ISREG (st_mode) a regular file
23. Is S_ISDIR (st_mode) a directory
24. Whether S_ISCHR (st_mode) is a character device
25. Whether S_ISBLK (st_mode) is a block device
26. Whether S_ISFIFO (st_mode) is a FIFO file
27. Is S_ISSOCK (st_mode) a SOCKET file?
If a directory has the sticky bit (S_ISVTX), it means that the files in this directory can only be deleted or renamed by the owner of the file, the owner of the directory, or root.

Second, fopen

header file stdio.h>
function prototype

FILE *fopen(const char *filename, const char *mode);

mode description

string description
r Open the file in read-only mode, the file must exist.
r + Open the file for read/write, the file must exist.
rb + Open a binary file for read/write, allowing only read/write data.
rt + Open a text file in read/write mode, allowing both reading and writing.
w Open the write-only file, if the file exists, the file length will be cleared to zero, that is, the file The content disappears; the file is created if it does not exist.
w + Open the read/write file, if the file exists, the file length will be cleared to zero, That is, the content of the file will disappear; if the file does not exist, the file will be created.
a Open the write-only file as an append. If the file does not exist, the file will be created; if the file exists, the written data will be added to the end of the file, that is, the original content of the file will be preserved (EOF character > reserved).
a + Open the file for read/write in append mode. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained (EOF character > not retained).
wb Open or create a binary file in write-only mode, only allowing data to be written.
wb + Open or create a binary file in read/write mode, allowing reading and writing.
wt + Open or create a new text file in read/write mode, allowing reading and writing.
at + Open a text file in read/write mode, allowing reading or appending at the end of the text data.
ab + Open a binary file in read/write mode, allowing reading or appending at the end of the file data.

Three, access

Header file: unistd.h
function prototype

int access(const char *filenpath, int mode);

Mode description, can add and subtract, up to 777

R_OK[4] only judges whether there is read permission
W_OK[2] Only judge whether there is write permission
X_OK[1] Determine whether there is execution permission
F_OK[0] only judges whether it exists

Fourth, sample code

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

void isFile(char *filename, int mode);
void isOpenFile(char *filename);
void isStatFile(char *filename);
int main()
{<!-- -->

printf("The folder exists and is writable\
");
isFile("/home/work/data", F_OK);
isFile("/home/work/filedata", F_OK);
printf("The folder exists and is writable\
");
isFile("/home/work/data/asd.txt", 7);
isFile("/home/work/data/qwe.txt", F_OK);
isOpenFile("/home/work/data/asd.txt");
isStatFile("/home/work/data/asd.txt");
return 0;

}
/*
R_OK[4] Only judge whether there is read permission
W_OK[2] Only judge whether there is write permission
X_OK[1] Determine whether there is execution permission
F_OK[0] only judges whether it exists\
"
*/
void isFile(char *filename, int mode)
{<!-- -->
printf("isFile start <<<<<<<<<<<<<<<<<<<<<<<<<\
");
int iRent = 0;

iRent = access(filename, mode);
if( 0 == iRent )
{<!-- -->
printf("file[%s] meets the permission requirements mode[%d][%d]\
", filename, mode, iRent);
}
else
{<!-- -->
printf("file[%s] does not meet the permission requirements mode[%d][%d]\
", filename, mode, iRent);
}
printf("isFile end >>>>>>>>>>>>>>>>>>>>>>>>>>>\
");

}

/*
|string | description |
|:--|:--|
|r | Open the file for reading only, the file must exist. |
|r + | opens a file for read/write, the file must exist. |
|rb + | Open a binary file for read/write, allowing only read/write data. |
|rt + | Open a text file in read/write mode, allowing both reading and writing. |
|w | Open a write-only file. If the file exists, the length of the file will be cleared to zero, that is, the content of the file will disappear; if the file does not exist, the file will be created. |
|w + | Open a readable/writable file. If the file exists, the length of the file will be cleared to zero, that is, the content of the file will disappear; if the file does not exist, the file will be created. |
|a | Opens the file for writing only as an append. If the file does not exist, the file will be created; if the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained (EOF character is retained). |
|a + | Open a file for reading/writing in append mode. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be preserved (the EOF character will not be preserved). |
|wb | Open or create a binary file for writing only, allowing only data to be written. |
|wb + | Open or create a binary file for read/write, allowing both reading and writing. |
|wt + | Open or create a new text file for read/write, allowing both reading and writing. |
|at + | Opens a text file for reading/writing, allowing data to be read or appended to the end of the text. |
|ab + | Open a binary file for reading/writing, allowing data to be read or appended to the end of the file. |
*/

void isOpenFile(char *filename)
{<!-- -->
printf("isOpenFile start <<<<<<<<<<<<<<<<<<<<<<<<<\
");
FILE *file = NULL;
file = fopen(filename, "r");
if(NULL == file)
{<!-- -->
printf("Failed to open the file\
");
}
else
{<!-- -->
printf("File opened successfully\
");
}
fclose(file);
printf("isOpenFile end >>>>>>>>>>>>>>>>>>>>>>>>>>>>\
");

}



void isStatFile(char *filename)
{<!-- -->
printf("isStatFile start <<<<<<<<<<<<<<<<<<<<<<<<<\
");
struct stat stfileStat;
memset( &stfileStat, 0x00, sizeof(stfileStat));

if(-1 == stat(filename, & stfileStat))
{<!-- -->
printf("judgment failed\
");
}
else
{<!-- -->
printf("stat.st_size[%d]\
", stfileStat.st_size);
\t
}
printf("isStatFile end >>>>>>>>>>>>>>>>>>>>>>>>>>>\
");
}