If you're on Linux or some other UNIX like system, what you want is the stat
function:
struct stat statbuf;
int rval;
rval = stat(path_to_file, &statbuf);
if (rval == -1) {
perror("stat failed");
} else {
printf("file size = %lld
", (long long)statbuf.st_size;
}
On Windows under MSVC, you can use _stati64
:
struct _stati64 statbuf;
int rval;
rval = _stati64(path_to_file, &statbuf);
if (rval == -1) {
perror("_stati64 failed");
} else {
printf("file size = %lld
", (long long)statbuf.st_size;
}
Unlike using fseek
, this method doesn't involve opening the file or seeking through it. It just reads the file metadata.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…