I'm new to C but trying some system calls.
I'm writing program that iterates through all files in a directory and prints the current file name and size. I can get the program to print the file name but it errors when I preform the stat system call.
Here is some of the code:
while (dptr = readdir(dirp)) {
if (stat(dptr->d_name, &buf) != 0) {
//Always does this and it does print the file name
printf("Error on when getting size of %s \n", dptr->d_name);
} else {
//Never gets here
printf("%u", buf.st_size);
}
}
I have the structs described like this:
struct stat buf;
struct dirent *dptr;
DIR *dirp;
If I change:
if (stat(dptr->d_name, &buf) != 0)
to
if (stat(dptr->d_name, &buf) != [EACCES])
It still goes into the loop which makes me think it can't read the file name but it's printing it in the error statement without a problem.
Can someone point me in the right direction? Thanks!
Аркадий