Currently, I have a program that lists the contents of a directory. What I need to do (as I've sort of started) is to use the stat structure to print such things as date modified, etc. like the "ls" program does. My code is as follows:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
int main()
{
char complete_filename[512];
struct dirent **namelist;
struct stat buf;
int n = scandir("./", &namelist, 0, alphasort);
int i;
for ( i = 0; i < n; i++ )
{
char *file_name = namelist[i]->d_name;
strcpy(complete_filename, "./");
strcat(complete_filename, "/");
strcat(complete_filename, file_name);
printf("%s\n", file_name);
}
return 0;
}
Basically, what I'm wondering is how I can use the stat structure to print such information. Can anyone help me out or at least point me in the right direction? (And the man pages are not much help for me).
If someone could just touch up my code so that it would print the date modified or something, so I could follow by example, that would be great.