linux - loop through a file and print file attributes in C -


i new programming in c. need program loop through files in folder , print these attributes each file. @ point printing attributes of folder.

#include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #include <stdio.h> #include <time.h> #include <dirent.h>  int main(int argc, char *argv[]) {     dir *dp;     struct stat file_stats;      if (argc != 2) {         fprintf(stderr, "usage: fstat file...\n");         return exit_failure;     }      if ((stat(argv[1], &file_stats)) == -1) {         perror("fstat");         return exit_failure;     }      dp = opendir("./");     if (dp == null) {         perror("couldn't open directory");         return exit_failure;     }      while (readdir(dp)) {         printf("filename: %s\n", argv[1]);         printf(" device: %lld\n",                 file_stats.st_dev);         printf(" protection: %o\n",                 file_stats.st_mode);         printf(" number of hard links: %d\n",                 file_stats.st_nlink);         printf(" user id of owner: %d\n",                 file_stats.st_uid);         printf(" group id of owner: %d\n",                 file_stats.st_gid);         printf(" device type (if inode device): %lld\n",                 file_stats.st_rdev);         printf(" total size, in bytes: %ld\n",                 file_stats.st_size);         printf(" blocksize filesystem i/o: %ld\n",                 file_stats.st_blksize);         printf(" inode number: %lu\n",                 file_stats.st_ino);         printf(" time of last access: %ld : %s",                 file_stats.st_atime,                 ctime(&file_stats.st_atime));         printf(" time of last change: %ld : %s",                 file_stats.st_ctime,                 ctime(&file_stats.st_ctime));         closedir(dp);     }      return exit_success; } 

i think need move struct while loop when compiler says "file_stats undeclared."

in addition valentin's answer, should move closedir() out of loop.

update: need replace stat(argv[1], ...) stat(ep->d_name, ...) obtain information actual file. before need enter argv[1] directory (using chdir() system call).

completely working code:

#include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #include <stdio.h> #include <time.h> #include <dirent.h> #include <unistd.h>  int main(int argc, char *argv[]) {     dir *dp;     struct stat file_stats;     struct dirent *ep;     int ret = exit_success;     int ret2;      if (argc != 2) {         fprintf(stderr, "usage: %s file...\n", argv[0]);         return exit_failure;     }      dp = opendir(argv[1]);     if (dp == null) {         perror("couldn't open directory");         return exit_failure;     }      ret2 = chdir(argv[1]);     if (ret2 == -1) {         perror("unable change directory");         ret = exit_failure;         goto out1;     }      while ((ep = readdir(dp))) {         printf("filename: %s\n", ep->d_name);          if ((stat(ep->d_name, &file_stats)) == -1) {             perror("fstat");             ret = exit_failure;             goto out2;         }          printf(" device: %lld\n",                 file_stats.st_dev);         printf(" protection: %o\n",                 file_stats.st_mode);         printf(" number of hard links: %d\n",                 file_stats.st_nlink);         printf(" user id of owner: %d\n",                 file_stats.st_uid);         printf(" group id of owner: %d\n",                 file_stats.st_gid);         printf(" device type (if inode device): %lld\n",                 file_stats.st_rdev);         printf(" total size, in bytes: %ld\n",                 file_stats.st_size);         printf(" blocksize filesystem i/o: %ld\n",                 file_stats.st_blksize);         printf(" inode number: %lu\n",                 file_stats.st_ino);         printf(" time of last access: %ld : %s",                 file_stats.st_atime,                 ctime(&file_stats.st_atime));         printf(" time of last change: %ld : %s\n",                 file_stats.st_ctime,                 ctime(&file_stats.st_ctime));     }  out2:     ret2 = chdir("..");     if (ret2 == -1) {         perror("unable change directory back");         ret = exit_failure;         goto out1;     }  out1:     closedir(dp);     return ret; } 

Popular posts from this blog