This content originally appeared on DEV Community and was authored by Naman Tamrakar
In this article I will discuss two approaches to get the file size using C programming language.
Approach 1
When we have to get the file size first approach that comes in our mind is to open the file, seek the cursor to the end of the file and then get position of cursor which is nothing but the size of file.
Let's understand how can we achieve this.
- First open file using
fopen. First argument is file path and second argument ismodeand it returns thefile descriptor. Since we don't have to modify file can open in read mode using"r". - Now move the cursor to the end of the file using
fseekwhich take following arguments respectively.-
fdfile descriptor of the file opened. -
offsethow much to offset with respect to third argument postion. -
whencefrom where the cursor should consider to move from. We have three option for it,SEEK_SETrelative to start of file,SEEK_CURrelative to current position andSEEK_ENDrelative to end of file.
-
Since we have to move to end of file we take relative to end of
file and offset as zero, as we have to move zero from end. fseek return -1 if it fails.
- Now use
ftellto get the current position which takesfdas argument and return current position which is our file size. On fail it return-1.
NOTE: If cursor move by 1 unit we consider it as 1 byte because the size of
charis 1 byte andfopenreads the file character by character only.
Code
#include <stdio.h>
// function get file size
long get_file_size(char *);
int main() {
char *filename = "a.out";
printf(
"Size of file `%s` is %ld\n",
filename,
get_file_size(filename)
);
return 0;
}
long get_file_size(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp==NULL)
return -1;
if (fseek(fp, 0, SEEK_END) < 0) {
fclose(fp);
return -1;
}
long size = ftell(fp);
// release the resources when not required
fclose(fp);
return size;
}
Size of file `a.out` is 51880 bytes
Approach 2
In this approach we use stat API to get the file information which also includes file size.
For this we simply call stat function which takes file path as first argument and pointer to stat struct variable as second argument and returns -1 on failure.
To get the file size we can simply use st_size attribute.
Using this we can get some other information of file such as permission, creation time, user id, group id, etc. Also there are some other function defined which can be used based on requirements. Checkout its man page for more info.
Code
#include <sys/stat.h> // stat
// inherit above base code here
long get_file_size(char *filename) {
struct stat file_status;
if (stat(filename, &file_status) < 0) {
return -1;
}
return file_status.st_size;
}
Size of file `a.out` is 51880 bytes
Which one should be used?
Second approach is more recommended because of following reason.
- Its fast as we don't have to open a file which in first approach have to be done. Some compiler may buffer a file on opening which makes first approach slower.
- The code is more clear to other developer that we are trying to get file info.
But this code is not portable. As other APIs are developed on OS like windows to get file info.
Whereas first approach code is portable. As same code works normally without any problem.
So In conclusion this is completely depends on our requirement which one approach should we use.
❤️Thank you so much for reading this article. I'm passionate engineering student learning new things so If you find any mistake or have any suggestion please let me know in comments.
Also consider sharing and giving a thumbs up If this post help you in any way.
This content originally appeared on DEV Community and was authored by Naman Tamrakar
Naman Tamrakar | Sciencx (2022-07-16T19:27:13+00:00) Ways to get the file size in C. Retrieved from https://www.scien.cx/2022/07/16/ways-to-get-the-file-size-in-c/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.