Linux under C gets the size of the file

Source: Internet
Author: User
Tags readable

Get file size There are two ways to do this:

Method One,

Example:

  1. Unsigned long get_file_size (const char *path)
  2. {
  3. unsigned long filesize =-1;
  4. FILE *FP;
  5. fp = fopen (Path, "R");
  6. if (fp = = NULL)
  7. return filesize;
  8. Fseek (FP, 0L, seek_end);
  9. FileSize = Ftell (FP);
  10. Fclose (FP);
  11. return filesize;
  12. }

This method of opening the file to obtain the size of the file, not suitable for large files, and may have an access violation (such as the file being downloaded), the efficiency is relatively low

Method Two,

Example:

  1. #include <sys/stat.h>
  2. Unsigned long get_file_size (const char *path)
  3. {
  4. unsigned long filesize =-1;
  5. struct stat statbuff;
  6. if (stat (path, &statbuff) < 0) {
  7. return filesize;
  8. }else{
  9. FileSize = statbuff.st_size;
  10. }
  11. return filesize;
  12. }

This method is used to read the file attributes to get the file size, high efficiency, and more stable

The details of the stat are pasted below:

Stat (get file status)
Correlation function Fstat,lstat,chmod,chown,readlink,utime

Table header file #include <sys/stat.h>
#include <unistd.h>

Defines the function int stat (const char * file_name,struct stat *buf);

The function Description Stat () is used to copy the file state referred to by the parameter file_name to the structure referred to in the parameter buf.
The following is a description of the parameters within the 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 number of the St_dev file
I-node of the St_ino file
Types of St_mode files and permissions to access them
St_nlink the number of hard connections to the file, the newly created file value is 1.
User ID of the St_uid file owner
St_gid file owner's group identification code
St_rdev If this file is an appliance device file, its device number
st_size file size, calculated in bytes
The I/O buffer size of the St_blksize file system.
St_blcoks occupies the number of file chunks, each chunk size is 512 bytes.
The time that the St_atime file was last accessed or executed is generally only changed with Mknod, Utime, read, write, and Tructate.
The last time the St_mtime file was modified is usually only changed when Mknod, Utime, and write are used.
St_ctime I-node The last time it was changed, this parameter updates the previously described St_mode when the file owner, group, and permissions are changed defines the following cases
S_ifmt 0170000 file Type bit mask
S_ifsock 0140000 Scoket
S_iflnk 0120000 Symbolic Connection
S_ifreg 1,000,001-like file
S_IFBLK 0060000 block Unit
S_ifdir 0040000 Catalogue
S_IFCHR 0020000-character device
S_ififo 0010000 Advanced First Out
S_isuid 04000 file (set User-id on execution) bit
S_isgid 02000 file (set Group-id on execution) bit
Sticky bits of s_isvtx 01000 file
S_IRUSR (s_iread) 00400 file owner with readable permissions
S_IWUSR (s_iwrite) 00200 file owner with writable permission
S_IXUSR (s_iexec) 00100 file owner with executable permissions
S_IRGRP 00040 user groups with readable permissions
S_IWGRP 00020 user groups with writable permissions
S_IXGRP 00010 user groups with executable permissions
S_iroth 00004 Other users have read access
S_iwoth 00002 Other users have writable permissions
S_ixoth 00001 other users with executable permissions
The above file types are defined in POSIX to check for these types of macro definitions
S_islnk (St_mode) determines whether a symbolic connection
S_isreg (St_mode) is a generic file
S_isdir (St_mode) is a directory
S_ISCHR (St_mode) is a character device file
S_ISBLK (s3e) is FIFO
S_issock (St_mode) is the socket
If a directory has a sticky bit (s_isvtx), then the file in this directory can only be deleted or renamed by the file owner, this directory owner, or root.

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

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);
}

Execute/etc/passwd File size = 705

Linux under C gets the size of the file

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.