Home>
int get_filesize (char * fname) {
    FILE * fp;
    int fsize;
    if ((fp = fopen (fname, "rb")) == NULL) {
        fprintf (stderr, "Error: Unable to input file '% s' \ n", fname);
        exit (8);
    }
    / * Get file size * /
    fseek (fp, 0L, SEEK_END);// move fp to the end of the file
    fsize = ftell (fp);// get the value of the current position of fp
    fclose (fp);
    return (fsize);
}


Why do this to get the file size
/* Get file size * /     fseek (fp, 0L, SEEK_END);// move fp to the end of the file     fsize = ftell (fp);// Get the value of the current position of fp
What is 0L in fseek?

I want you to know who I understand

c
  • Answer # 1

      

    fseek (fp, 0L, SEEK_END);

    0L is a long 0
    Since it moves to the position based on the end of the file, 0 is specified, so it moves to the end of the file.
    So, if you ask for that position, you will be asked for the file size naturally.

    fseekandftell… Becausenot guaranteed to work properly(JPCERT/CC).

    Use another method to "get file information", such as

    fstat.

Related articles