How do you determine the size of a file in C?

前端 未结 14 1283
野性不改
野性不改 2020-11-22 03:20

How can I figure out the size of a file, in bytes?

#include 

unsigned int fsize(char* file){
  //what goes here?
}
14条回答
  •  忘掉有多难
    2020-11-22 03:44

    I used this set of code to find the file length.

    //opens a file with a file descriptor
    FILE * i_file;
    i_file = fopen(source, "r");
    
    //gets a long from the file descriptor for fstat
    long f_d = fileno(i_file);
    struct stat buffer;
    fstat(f_d, &buffer);
    
    //stores file size
    long file_length = buffer.st_size;
    fclose(i_file);
    

提交回复
热议问题