Thread: Using sys/stat.h and listing directory contents in C...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Maine
    Posts
    11

    Question Using sys/stat.h and listing directory contents in C...

    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.

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Hi

    Something like this should work!
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main()
    {
            struct stat *buf;
    
            if ((lstat("./fbsd.txt",buf))==-1)
            {
                    perror("lstat");
                    exit(1);
            }
    
            printf("UID : %d\nGID : %d\n",buf->st_uid,buf->st_gid);
    
            return 0;
    }
    Your code does not employ buf variable!

    >(And the man pages are not much help for me).
    Sure...I suggest you to read them carefully!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > strcat(complete_filename, file_name);
    Follow with
    Code:
    struct stat sb;
    stat ( complete_filename, &sb );
    printf( "File is %lu bytes\n", sb.st_size );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. On reading Directory contents and listing them.
    By Deathfrost in forum C Programming
    Replies: 9
    Last Post: 07-14-2009, 08:21 AM
  2. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 05:42 AM
  3. Replies: 4
    Last Post: 06-05-2006, 03:24 AM
  4. Directory listing in treeview control
    By Hunter2 in forum Windows Programming
    Replies: 1
    Last Post: 06-23-2003, 04:52 PM
  5. Directory listing
    By brif in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2002, 06:44 AM