learn through suffering C language learn through suffering 
C language

How to Get File Size

I want to know the file size. Could you tell me a function for that?

There is no function in C to determine file size.
To determine the file size, you need to open the file, move the file pointer, and then retrieve the size.

sum.c
fpos_t GetFileSize(const char* FileName[])
{
    fpos_t fsize = 0;

    FILE* fp = fopen(FileName, "rb");

    /* Investigate file size */
    fseek(fp, 0, SEEK_END);
    fgetpos(fp, &fsize);

    fclose(fp);

    return fsize;
}

`fpos_t` is a variable representing the size of data, and it is essentially an unsigned integer value.Converting it to an int won't be a big deal.
Also, the file pointer, or the current read position, changes during the process described above, so it needs to be reset as follows.

sum.c
fpos_t fsize = 0;
fpos_t fsizeb =  fseek(fp,0,SEEK_END);
fgetpos(fp,&fsize);
fseek(fp,fsizeb,SEEK_SET);



About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.


Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. line break
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19

Loading comment system...