I am trying to create a function that will take an inputted directory path (filrOrDir) and output info for each file in the directory: file name, size, and last access date. T
name = dir->d_name
is the name of the file inside the directory fileOrDir
, but
stat(name, buffer);
tries to stat the file name
in the current working directory.
That fails (unless fileOrDir
happens to be the current working directory),
and therefore the contents of buffer
is undetermined.
You have to concatenate the directory and the file name for the stat call. You should also check the return value of the stat call. For example:
char fullpath[MAXPATHLEN];
snprintf(fullpath, sizeof(fullpath), "%s/%s", fileOrDir, name);
if (stat(fullpath, buffer) == -1) {
printf(stderr, "stat failed: %s\n", strerror(errno));
} else {
// print access time etc.
}