We already saw with the mmap function how to do this using the fstat function.
1 #include <unistd.h> 2 #include <sys/stat.h> 3 4 int main() 5 { 6 struct stat file_stat; 7 8 stat("foo.txt", &file_stat); 9 10 printf("File foo.txt has %u bytes\n", file_stat.st_size); 11 12 13 }
The stat function returns -1 for error.
Instead of using the stat function with the file name, there is a function, fstat that can be used if you have already opened the file and have a file descriptor, fd for the file.
8 fstat(fd, &file_stat);