C获取文件大小stat()

NAME

stat - get file status

SYNOPSIS

#include <sys/stat.h>

int stat(const char *restrict
path, struct stat *restrict buf);

DESCRIPTION

The stat() function shall obtain information about the named file and write it to the area pointed to by the buf argument. The path argument points to a pathname naming a file. Read, write, or execute permission of the named file is not required. An implementation that provides additional or alternate file access control mechanisms may, under implementation-defined conditions, cause stat() to fail. In particular, the system may deny the existence of the file specified by path.

If the named file is a symbolic link, the stat() function shall continue pathname resolution using the contents of the symbolic link, and shall return information pertaining to the resulting file if the file exists.

The buf argument is a pointer to a stat structure, as defined in the <sys/stat.h> header, into which information is placed concerning the file.

The stat() function shall update any time-related fields (as described in the Base Definitions volume of IEEE Std 1003.1-2001, Section 4.7, File Times Update), before writing into the stat structure.

Unless otherwise specified, the structure members st_mode, st_ino, st_dev, st_uid, st_gid, st_atime, st_ctime, and st_mtime shall have meaningful values for all file types defined in this volume of IEEE Std 1003.1-2001. The value of the member st_nlink shall be set to the number of links to the file.

RETURN VALUE

Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and errno set to indicate the error.

1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 
5 struct stat buffer;
6 int         status;
7 ...
8 status = stat("/home/cnd/mod1"&buffer);

来自:http://www.opengroup.org/onlinepubs/000095399/functions/stat.html

代码
struct stat
{
    _dev_t    st_dev;        
/* Equivalent to drive number 0=A 1=B ... */
    _ino_t    st_ino;        
/* Always zero ? */
    _mode_t    st_mode;    
/* See above constants */
    
short    st_nlink;    /* Number of links. */
    
short    st_uid;        /* User: Maybe significant on NT ? */
    
short    st_gid;        /* Group: Ditto */
    _dev_t    st_rdev;    
/* Seems useless (not even filled in) */
    _off_t    st_size;    
/* File size in bytes */
    time_t    st_atime;    
/* Accessed date (always 00:00 hrs local
                 * on FAT) 
*/
    time_t    st_mtime;    
/* Modified time */
    time_t    st_ctime;    
/* Creation time */
};
原文地址:https://www.cnblogs.com/yanhc/p/1635818.html