C Language: Stat,fstat and Lstat functions

Source: Internet
Author: User
Tags lstat

The functions of these three functions are consistent and are used to obtain file-related information, but apply to different file objects. For the pathname parameter given in the function, the STAT function returns the information structure associated with this named file, the Fstat function obtains information about the file that has been opened on the descriptor fields, and the Lstat function is similar to stat but when the named file is a symbolic link, Lstat returns information about the symbolic link instead of referencing the file by the symbolic link. The second parameter, buf, is a pointer to a structure that holds the file description information, and the function fills in the structure content. The actual definition of the structure may vary depending on the implementation.

Usage:

#include int stat (constcharstruct stat *buf); int fstat (intstruct stat *buf); int lstat (constcharstruct stat *buf);

Parameters:
Path: File path name.
Filedes: File description Word.
BUF: is a pointer to the following struct body


struct stat{
mode_t St_mode; (File Protection mode) file type and permission information structure in detail please refer here
ino_t St_ino; File node number
dev_t St_dev; File system identification number of the device on which the file resides
dev_t St_rdev; Device ID for the special device file represented by the file devices number for special files
nlink_t St_nlink; Number of Symbolic links
uid_t St_uid; File user identity User ID
gid_t St_gid; File User Group identity Group ID
off_t st_size; Total size, bytes in size in bytes,for regular files
time_t St_st_atime; The last time the file content was accessed
time_t St_mtime; File content Last modified time
time_t St_ctime; File structure last state change time
blksize_t st_blksize; Optimal I/O block size of the file system
blkcnt_t st_blocks; The number of blocks allocated to a file, 512 bytes is 1 cells, # of disk blocks allocated
};

File type: the
Ordinary files, directory files, block special files, character special files, sockets, FIFO, symbolic links.
File type information is included in the St_mode member of the stat structure and can be used to determine the file type, which is a member of St_mode in the stat structure.
S_isreg ();

S_isdir ();

S_isblk ();

S_ISCHR ();

S_issock ();

S_isfifo ();

S_islnk ();

Return Description:
When executed successfully, returns 0. The failed return -1,errno is set to one of the following values
EBADF: Invalid file description
Efault: Address space not accessible
Eloop: Too many symbolic connections are encountered while traversing paths
Enametoolong: File path name is too long
ENOENT: A partial component of the pathname does not exist, or the path name is an empty string
ENOMEM: Low Memory
Enotdir: A partial component of a pathname is not a directory

Example:

#includeintMainintargcChar*argv[]) {    inti; structstat buf; Char*ptr;  for(i=1; I {if(Lstat (ARGV[I],&AMP;BUF) <0{perror ("The cause of the error is:"); Continue; }        if(S_isreg (buf.st_mode)) PTR="Ordinary Documents"; if(S_isdir (buf.st_mode)) PTR="Catalogue"; //... and so on ...cout<< "parameter:" <<< "is an identity that is a" <<} exit (0);}

C Language: Stat,fstat and Lstat functions

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.