From Baidu Library: Http://wenku.baidu.com/view/31777dc1d5bbfd0a795673b1.htmlstat function Explanation:
Table header files: #include <sys/stat.h>
#include <unistd.h>
Define functions: Int stat (const char *file_name, struct stat *buf);
Function Description: Obtains file information by filename, and is stored in the structure stat referred to by buf
Return value: Execution succeeds returns 0, failure returns-1, error code stored in errno
Error code:
ENOENT parameter file_name The specified file does not exist
Directories in the Enotdir path exist but are not real directories
Eloop the file you want to open has too many symbolic connection problems, with a maximum of 16 symbolic connections
Efault parameter BUF is invalid pointer, pointing to memory space that cannot exist
Eaccess denied when accessing file
Enomem Core memory is low
Enametoolong parameter file_name The path name is too long
int stat (const char *file_name,struct stat *buf);
int fstat (int filedes,struct stat *buf);
The stat structure is defined in:/usr/include/sys/stat.h file
struct stat finfo;
Stat (sFileName, &finfo);
int size = Finfo. St_size;
struct STAT {
mode_t St_mode; File corresponding to the mode, file, directory, etc.
ino_t St_ino; I-node Node Number
dev_t St_dev; Device number
dev_t St_rdev; Special Equipment Number
nlink_t St_nlink; Number of connections to the file
uid_t St_uid; File owner
gid_t St_gid; The group that the file owner corresponds to
off_t st_size; Normal file, corresponding file byte number
time_t St_atime; Time the file was last visited
time_t St_mtime; time when the contents of the file were last modified
time_t St_ctime; File status (attribute) change time
blksize_t st_blksize; Block size for file contents
blkcnt_t st_blocks; Number of blocks corresponding to the file contents
};
Stat is used to determine which files are not open, and fstat is used to determine which files are open. The most common attribute we use is st_mode. By using properties we can tell whether a given file is a normal file or a directory, a connection, and so on. You can use the following macros to judge.
S_islnk (St_mode): Whether it is a connection.
Whether the S_isreg is a regular file.
Whether the S_isdir is a directory
Whether the S_ISCHR is a character device.
Whether the S_ISBLK is a block device
Whether the S_isfifo is a FIFO file.
Whether the S_issock is a socket file.
Usage:
Define a struct body
struct stat m;
The file name entered by the user (to determine if it is a directory).
Char *filename;
int A;
int n = stat (filename,&m);
A = S_isdir (M.st_mode);
If A is true, the description is the directory, otherwise it is not.
S_isreg a few common macros (GO)