Solution for Exercise 23-3
ex2303
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include <sys/stat.h>
int main()
{
DIR *folder;
struct dirent *file;
struct stat filestat;
folder=opendir(".");
if(folder==NULL)
{
puts("Unable to read directory");
exit(1);
}
while(file = readdir(folder))
{
stat(file->d_name,&filestat);
printf("%-14s %5ld %s",
file->d_name,
(long)filestat.st_size,
ctime(&filestat.st_mtime));
}
closedir(folder);
return(0);
}
Notes
* If you don't typecast the filestat.st_size variable at Line 24, then the compiler pukes out a warning error. The code still compiles and runs.
* You can change the width values at Line 22 if the output looks too cluttered.
* See the solution for Exercise 23-2 for inforamtion on any "suggest parentheses" warnings you may see.
Copyright © 1997-2025 by QPBC.
All rights reserved
