All i want is to print the size of a file in bytes I am using
DIR *d;
struct dirent *dir;
d= opendir(absolute_path);
while((dir= readdir(d))!=NULL){
printf("%s\t %d\t %u\n",dir->d_name,dir->d_type,(int long long )dir->d_off);
}
The printing of the d_off that is type off_t
is wrong.
For a file of 323,388 bytes
it prints 1296623584
I think that the casting is the problem. I tried lots of castings such as %d
, %s
,%u
, %llu
...
What is the right casting?
EDIT: The right function to use to find filesize is lstat()
using a stat struct.
As @Andrey points out, the value of d_off is not always useful or even present. OSX does not have it for instance. Use a call to
stat
instead.