This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Ways to get the file size in C


On this article I’ll talk about two approaches to get the file dimension utilizing C programming language.



Strategy 1

When we now have to get the file dimension first strategy that is available in our thoughts is to open the file, search the cursor to the tip of the file after which get place of cursor which is nothing however the dimension of file.

Let’s perceive how can we obtain this.

  • First open file utilizing fopen. First argument is file path and second argument is mode and it returns the file descriptor. Since we do not have to change file can open in learn mode utilizing "r".
  • Now transfer the cursor to the tip of the file utilizing fseek which take following arguments respectively.
    • fd file descriptor of the file opened.
    • offset how a lot to offset with respect to 3rd argument postion.
    • whence from the place the cursor ought to contemplate to maneuver from.
      Now we have three choice for it, SEEK_SET relative to begin of
      file, SEEK_CUR relative to present place and SEEK_END
      relative to finish of file.

Since we now have to maneuver to finish of file we take relative to finish of
file and offset as zero, as we now have to maneuver zero from finish. fseek return -1 if it fails.

  • Now use ftell to get the present place which takes fd as argument and return present place which is our file dimension. On fail it return -1.

NOTE: If cursor transfer by 1 unit we contemplate it as 1 byte as a result of the scale of char is 1 byte and fopen reads the file character by character solely.

Code

#embody <stdio.h>

// operate get file dimension
lengthy get_file_size(char *);

int important() {
    char *filename = "a.out";
    printf(
        "Measurement of file `%s` is %ldn", 
        filename, 
        get_file_size(filename)
    );

    return 0;
}

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

    lengthy dimension = ftell(fp);
    // launch the assets when not required
    fclose(fp);
    return dimension;
}
Enter fullscreen mode

Exit fullscreen mode

Measurement of file `a.out` is 51880 bytes
Enter fullscreen mode

Exit fullscreen mode



Strategy 2

On this strategy we use stat API to get the file data which additionally consists of file dimension.

For this we merely name stat operate 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 dimension we will merely use st_size attribute.

Utilizing this we will get another data of file corresponding to permission, creation time, consumer id, group id, and many others. Additionally there are another operate outlined which can be utilized based mostly on necessities. Checkout its man page for more information.

Code

#embody <sys/stat.h> // stat

// inherit above base code right here

lengthy get_file_size(char *filename) {
    struct stat file_status;
    if (stat(filename, &file_status) < 0) {
        return -1;
    }

    return file_status.st_size;
}
Enter fullscreen mode

Exit fullscreen mode

Measurement of file `a.out` is 51880 bytes
Enter fullscreen mode

Exit fullscreen mode



Which one needs to be used?

Second strategy is extra really useful due to following motive.

  • Its quick as we do not have to open a file which in first strategy should be executed. Some compiler might buffer a file on opening which makes first strategy slower.
  • The code is extra clear to different developer that we try to get file information.

However this code isn’t transportable. As different APIs are developed on OS like home windows to get file information.

Whereas first strategy code is transportable. As identical code works usually with none downside.

So In conclusion that is fully relies on our requirement which one strategy ought to we use.


❤️Thanks a lot for studying this text. I am passionate engineering pupil studying new issues so If you happen to discover any mistake or have any suggestion please let me know in feedback.

Additionally contemplate sharing and giving a thumbs up If this publish aid you in any manner.

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?