How to get the file length in C on Linux

How to get the file length in C on Linux given the address of the file (e.g. “/tmp/a.txt”)?

This function returns the length of the file:

#include <sys/stat.h>

long file_length(char *f)
{
    struct stat st;
    stat(f, &st);
    return st.st_size;
}
Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

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