Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 16, 2022 07:27 pm GMT

Ways to get the file size in C

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 is mode and it returns the file 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 fseek which take following arguments respectively.
    • fd file descriptor of the file opened.
    • offset how much to offset with respect to third argument postion.
    • whence from where the cursor should consider to move from. We have three option for it, SEEK_SET relative to start of file, SEEK_CUR relative to current position and SEEK_END relative 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 ftell to get the current position which takes fd as 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 char is 1 byte and fopen reads the file character by character only.

Code

#include <stdio.h>// function get file sizelong get_file_size(char *);int main() {    char *filename = "a.out";    printf(        "Size of file `%s` is %ld
", 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 herelong 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.


Original Link: https://dev.to/namantam1/ways-to-get-the-file-size-in-c-2mag

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To