How To Get The Size Of A File In C
Get File Size in C
- Apply the
stat
Function to Go File Size in C - Use
fstat
Role to Get File Size in C
This article will explicate several methods of how to become file size in C.
Apply the stat
Office to Get File Size in C
stat
organization call is the POSIX compliant function that can be used to call up various attributes of the given file. It takes two arguments - the start of which is the char
pointer that should point to the file's pathname, and the 2nd statement is of type struct stat
pointer which is described extensively in the role manual. Once the part telephone call returns successfully, the user tin can extract the needed attributes of the file from the struct stat
by accessing its members direct. The file size is stored in the st_size
fellow member of the structure. Note that, stat
function does not require permissions on the file to retrieve the attributes, simply the caller should have executed permission on every directory included in the pathname.
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <fourth dimension.h> #include <sys/stat.h> const char *filename = "input.txt"; int primary(int argc, char *argv[]) { struct stat sb; if (stat(filename, &sb) == -1) { perror("stat"); exit(EXIT_FAILURE); } printf("Inode number: %lu\n", sb.st_ino); printf("User ID of owner: %u\n", sb.st_uid); printf("Group ID of owner: %u\n", sb.st_gid); printf("Total file size: %lu bytes\northward", sb.st_size); printf("Last status change: %s", ctime(&sb.st_ctime)); printf("Last file access: %s", ctime(&sb.st_atime)); printf("Terminal file modification: %southward", ctime(&sb.st_mtime)); get out(EXIT_SUCCESS); }
Output:
Inode number: 12638061 User ID of owner: one thousand Group ID of owner: thousand Total file size: 126 bytes Last condition change: Saturday Feb 6 23:35:02 2021 Terminal file access: Sat Feb 6 23:35:02 2021 Terminal file modification: Saturday Feb half-dozen 23:35:02 2021
The st_size
fellow member represents the total size of the file in bytes. In example the given pathname is the symbolic link, this member denotes the length of the pathname pointer by the link. We implement an example that takes the filename from the const
declared hardcoded string value in the higher up code sample, although the user may laissez passer the filename as the plan parameter as demonstrated in the following case. stat
telephone call returns -1
on failure and errno
is set accordingly; otherwise nix is returned to announce the success.
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <fourth dimension.h> #include <sys/stat.h> int main(int argc, char *argv[]) { struct stat sb; char *filename = Zilch; if (argc != 2) { printf("Usage: ./program filename\n"); go out(EXIT_FAILURE); } filename = argv[one]; if (stat(filename, &sb) == -i) { perror("stat"); exit(EXIT_FAILURE); } printf("Inode number: %lu\n", sb.st_ino); printf("User ID of possessor: %u\n", sb.st_uid); printf("Group ID of owner: %u\n", sb.st_gid); printf("Total file size: %lu bytes\n", sb.st_size); printf("Final condition alter: %southward", ctime(&sb.st_ctime)); printf("Last file access: %south", ctime(&sb.st_atime)); printf("Last file modification: %s", ctime(&sb.st_mtime)); go out(EXIT_SUCCESS); }
Employ fstat
Part to Get File Size in C
Alternatively, nosotros can use the fstat
part, which takes the file descriptor as the first argument to specify the target file. The file descriptor tin be acquired by open
system call, but exist sure to close opened files when they are not needed using the close
system call. Notice that all examples in this article impress most of the struct stat
members. You lot might need to extract the relevant information about the attributes using the additional functions, east.g. st_mode
and st_dev
.
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <fourth dimension.h> #include <sys/stat.h> const char *filename = "input.txt"; int main(int argc, char *argv[]) { struct stat sb; int fd = open(filename, O_RDONLY); if (fd == -i) { perror("open"); exit(EXIT_FAILURE); } if (fstat(fd, &sb) == -1) { perror("stat"); exit(EXIT_FAILURE); } printf("Inode number: %lu\n", sb.st_ino); printf("User ID of possessor: %u\n", sb.st_uid); printf("Group ID of owner: %u\n", sb.st_gid); printf("Total file size: %lu bytes\n", sb.st_size); printf("Last status change: %s", ctime(&sb.st_ctime)); printf("Terminal file access: %south", ctime(&sb.st_atime)); printf("Last file modification: %s", ctime(&sb.st_mtime)); close(fd); leave(EXIT_SUCCESS); }
Write for united states
DelftStack articles are written past software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you tin can cheque the write for united states of america page.
Related Commodity - C File
How To Get The Size Of A File In C,
Source: https://www.delftstack.com/howto/c/file-size-in-c/
Posted by: antonkeeduke.blogspot.com
0 Response to "How To Get The Size Of A File In C"
Post a Comment