////////////////////////////////////////////////
Exercise: Implement list all file attributes in a directory (file size, file last modified time, filename)
The directory name is passed in by the parameter./dir/home/linux
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
DIR *DP; Define a struct variable to open the file directory and return a pointer to the DIR struct
DP = Opendir (argv[1]);
struct Dirent *ep; Used to save a file This file contains the names of other files and pointers to information related to those files
EP = Readdir (DP); Readdir, read a file from the directory
while (ep! = NULL)
{
if (ep->d_name[0]! = '. ')
{
Char path[100] = {0};
struct STAT s;
sprintf (Path, "%s/%s", argv[1], ep->d_name);///home/linux/a.txt
int ret = stat (path, &s);
if (ret >= 0)
{
printf ("%s:%d:%s\n", Ep->d_name, (int) s.st_size,asctime (localtime (&s.st_atime)));
}
}
EP = Readdir (DP);
}
}
/////////////////////////////////////
Linux file IO directory operations and file attributes