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)) != NULL ) { stat(file->d_name,&filestat); printf("%-14s %5ld %s", file->d_name, filestat.st_size, ctime(&filestat.st_mtime)); } closedir(folder); return(0); }
Output
. 384 Thu May 21 21:27:11 2020
.. 832 Thu May 21 15:08:14 2020
listing2305.c 355 Thu May 21 15:36:29 2020
ex2303.c 564 Thu May 21 21:27:10 2020
listing2301.c 350 Thu May 21 15:32:52 2020
listing2306.c 222 Thu May 21 15:37:01 2020
listing2302.c 558 Thu May 21 21:22:12 2020
ex2301.c 350 Thu May 21 20:49:38 2020
a.out 12924 Thu May 21 21:27:11 2020
listing2303.c 357 Thu May 21 15:34:55 2020
ex2302.c 360 Thu May 21 20:57:01 2020
listing2304.c 381 Thu May 21 15:35:48 2020
Notes
* On the Macintosh, you may see a warning for Line 24. In OS X, the st_size
member of the stat
structure is a long long data type. To address this error, typecast the filestat.st_size
variable at Line 24 as follows:
* You can change the width values at Line 22 if the output looks too cluttered.
Copyright © 1997-2024 by QPBC.
All rights reserved