Obtain the file size in Linux C.

Source: Internet
Author: User

There are two methods to obtain the file size:

Method 1,

Example:

Unsigned long get_file_size (const char * path) <br/>{< br/> unsigned long filesize =-1; <br/> file * FP; <br/> fp = fopen (path, "R"); <br/> If (FP = NULL) <br/> return filesize; <br/> fseek (FP, 0l, seek_end); <br/> filesize = ftell (FP); <br/> fclose (FP); <br/> return filesize; <br/>}</P> <p>

 

In this way, the file size is obtained by opening the file, which is not suitable for large files and may cause access conflicts (such as files being downloaded), which is less efficient.

 

Method 2,

Example:

# Include <sys/STAT. h> </P> <p> unsigned long get_file_size (const char * path) <br/>{< br/> unsigned long filesize =-1; <br/> struct stat statbuff; <br/> If (STAT (path, & statbuff) <0) {<br/> return filesize; <br/>} else {<br/> filesize = statbuff. st_size; <br/>}< br/> return filesize; <br/>}

 

This method reads the file attributes to obtain the file size, which is efficient and stable.

 

 

Paste the details of stat below:

 

Stat (Get File status)
Related functions: fstat, lstat, chmod, chown, readlink, and utime

Header file # include <sys/STAT. h>
# Include <unistd. h>

Define the function int Stat (const char * file_name, struct stat * BUF );

Function Description Stat () is used to copy the state of the file referred to by the parameter file_name to the structure referred to by the parameter Buf.
The following describes the parameters in struct stat.
Struct stat
{
Dev_t st_dev;/* Device */
Ino_t st_ino;/* inode */
Mode_t st_mode;/* Protection */
Nlink_t st_nlink;/* Number of hard links */
Uid_t st_uid;/* User ID of owner */
Gid_t st_gid;/* Group ID of owner */
Dev_t st_rdev;/* device type */
Off_t st_size;/* Total size, in bytes */
Unsigned long st_blksize;/* blocksize for filesystem I/O */
Unsigned long st_blocks;/* number of blocks allocated */
Time_t st_atime;/* Time of lastaccess */
Time_t st_mtime;/* time of last modification */
Time_t st_ctime;/* time of last change */
};
Device ID of the st_dev File
I-node of the st_ino File
St_mode file type and access permission
The number of hard connections st_nlink connects to the file. The value of the created file is 1.
User ID of the st_uid file owner
Group ID of the st_gid file owner
St_rdev if this file is a device file, it is its device number
Size of the st_size file, in bytes
The size of the I/O buffer of the st_blksize file system.
The number of file blocks occupied by st_blcoks. The size of each block is 512 bytes.
The last time the st_atime file was accessed or executed, which is generally changed only when mknod, utime, read, write, and tructate are used.
The last modification time of the st_mtime file, which is generally changed only when mknod, utime, and write are used.
The last time st_ctime I-node was changed. When the file owner, group, and permission are changed, this parameter updates the previously described st_mode, which defines the following conditions:
S_ifmt 0170000 bit masks for file types
S_ifsock0140000 scoket
S_iflnk 0120000 symbolic connection
S_ifreg 0100000 general file
S_ifblk 0060000 block Device
S_ifdir 0040000 directory
S_ifchr 0020000 character device
S_ififo 0010000 first-in-first-out
S_isuid 04000 file (Set User-ID on execution) Bit
S_isgid 02000 (set group-ID on execution) bit of the file
S_isvtx 01000 file Sticky Bit
S_irusr (s_iread) 00400 the file owner has the read permission
S_iwusr (s_iwrite) 00200 the file owner has the write permission
S_ixusr (s_iexec) 00100 the file owner has executable permissions
S_irgrp 00040 user group with read permission
S_iwgrp 00020 user group with write permission
S_ixgrp 00010 user group with executable permissions
S_iroth 00004 other users have read permission
S_iwoth 00002 other users have write permission
S_ixoth 00001 other users have executable permissions
The above file type defines in POSIX to check the macro definitions of these types
S_islnk (st_mode) determines whether it is a symbolic connection
S_isreg (st_mode) is a normal file
S_isdir (st_mode) is a directory
S_ischr (st_mode) is a character Device File
S_isblk (s3e) for FIFO
S_issock (st_mode) is socket
If a directory has a sticky bit (s_isvtx), the files under this directory can only be deleted or renamed by the file owner, the directory owner, or root.

If the return value is successfully executed, 0 is returned. If the return value is failed,-1 is returned. The error code is stored in errno.

Error Code: The file specified by the enoent parameter file_name does not exist.
The directory in the enotdir path exists but is not a real directory
The file to be opened by eloop has too many symbolic connections. The upper limit is 16.
The efault parameter Buf is an invalid pointer and points to the memory space that cannot exist.
The eaccess is denied when accessing the file.
Insufficient enomem core memory
The path name of the enametoolong parameter file_name is too long.

Example # include <sys/STAT. h>
# Include <unistd. h>
Mian ()
{
Struct stat Buf;
Stat ("/etc/passwd", & BUF );
Printf ("/etc/passwd file size = % d/N", Buf. st_size );
}

Run/etc/passwd file size = 705

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.