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

前端 未结 14 932
野性不改
野性不改 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:41

    POSIX

    The POSIX standard has its own method to get file size.
    Include the sys/stat.h header to use the function.

    Synopsis

    • Get file statistics using stat(3).
    • Obtain the st_size property.

    Examples

    Note: It limits the size to 4GB. If not Fat32 filesystem then use the 64bit version!

    #include <stdio.h>
    #include <sys/stat.h>
    
    int main(int argc, char** argv)
    {
        struct stat info;
        stat(argv[1], &info);
    
        // 'st' is an acronym of 'stat'
        printf("%s: size=%ld\n", argv[1], info.st_size);
    }
    
    #include <stdio.h>
    #include <sys/stat.h>
    
    int main(int argc, char** argv)
    {
        struct stat64 info;
        stat64(argv[1], &info);
    
        // 'st' is an acronym of 'stat'
        printf("%s: size=%ld\n", argv[1], info.st_size);
    }
    

    ANSI C (standard)

    The ANSI C doesn't directly provides the way to determine the length of the file.
    We'll have to use our mind. For now, we'll use the seek approach!

    Synopsis

    • Seek the file to the end using fseek(3).
    • Get the current position using ftell(3).

    Example

    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        FILE* fp = fopen(argv[1]);
        int f_size;
    
        fseek(fp, 0, SEEK_END);
        f_size = ftell(fp);
        rewind(fp); // to back to start again
    
        printf("%s: size=%ld", (unsigned long)f_size);
    }
    

    If the file is stdin or a pipe. POSIX, ANSI C won't work.
    It will going return 0 if the file is a pipe or stdin.

    Opinion: You should use POSIX standard instead. Because, it has 64bit support.

    0 讨论(0)
  • 2020-11-22 03:42

    You can open the file, go to 0 offset relative from the bottom of the file with

    #define SEEKBOTTOM   2
    
    fseek(handle, 0, SEEKBOTTOM)  
    

    the value returned from fseek is the size of the file.

    I didn't code in C for a long time, but I think it should work.

    0 讨论(0)
  • 2020-11-22 03:43

    If you're fine with using the std c library:

    #include <sys/stat.h>
    off_t fsize(char *file) {
        struct stat filestat;
        if (stat(file, &filestat) == 0) {
            return filestat.st_size;
        }
        return 0;
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-22 03:45

    I have a function that works well with only stdio.h. I like it a lot and it works very well and is pretty concise:

    size_t fsize(FILE *File) {
        size_t FSZ;
        fseek(File, 0, 2);
        FSZ = ftell(File);
        rewind(File);
        return FSZ;
    }
    
    0 讨论(0)
  • 2020-11-22 03:46

    Here's a simple and clean function that returns the file size.

    long get_file_size(char *path)
    {
        FILE *fp;
        long size = -1;
        /* Open file for reading */
        fp = fopen(path, "r");
        fseek(fp, 0, SEEK_END);
        size = ftell(fp); 
        fp.close();
        return 
    }
    
    0 讨论(0)
提交回复
热议问题