ANSI C - list directory contents

Posted on 2005-05-14
Last Modified: 2008-01-09

Can anyone provide an example in ANSI C of how to list contents of a directory


Question by:nerdmike
    LVL 25

    Expert Comment


    Author Comment

    Those were all unix specific solutions

    I need ANSI C *ONLY*
    LVL 45

    Accepted Solution

    Hi nerdmike,

    There is no ANSI C solution to your question since there is no such API specified by ANSI. You have POSIX standard solution which is what you would see in *nix machines (readdir etc.), or there is a windows solution using findNextFile (or something similar). If you are interested in details for either of them, let me know and I can explain them.

    Once  again, ANSI C does not specify APIs for directory listings


    Author Comment


    I'm writing code for windows, but I try to keep it ANSI wherever possible.

    An example using the win32 api would be nice.

    LVL 45

    Expert Comment

    I can understand your frustration ... As it turns out, ANSI C supports almost nothing to interact with the system and windows is not POSIX compliant


    Expert Comment

    Here's a simple directory routine that works well with win32.  It prints all of the attributes available to you (via the _finddata_t structure) and the time of the last write to the file.  There are two other times available on NTFS systems they are create time and the last access time.  I have comments in the code that show how to read them.

    Based on the _A_SUBDIR attribute you might want to build two lists (one of files and one of directorys).  Also if you are displaying the list directly to the user you might consider not displaying _A_HIDDEN and _A_SYSTEM files, although I always prefer to see them.

    Hope this helps


    #include <io.h>
    #include "stdio.h"
    #include "time.h"

    void listDir(char *path)
          struct _finddata_t fileInfo;
          intptr_t fPtr;
          char attribs[8];
          char timeBuff[32];

          printf(" checking path : %s\n",path);
          if((fPtr = _findfirst(path,  &fileInfo )) == -1L)

                      // decode the archive flag
                      attribs[0] =( fileInfo.attrib & _A_ARCH )   ? 'A' : '.';
                      // decode the read only flag
                      attribs[1] =( fileInfo.attrib & _A_RDONLY ) ? 'R' : '.';
                      // decode the hidden file flag
                      attribs[2] = ( fileInfo.attrib & _A_HIDDEN ) ? 'H' : '.';
                      // decode the System file flag
                      attribs[3] =( fileInfo.attrib & _A_SYSTEM ) ? 'S' : '.';
                            // for readability
                      attribs[4] = '-';
                      // is this a subdirectory 'D' or a File 'F'
                      attribs[5] = (fileInfo.attrib       & _A_SUBDIR ) ? 'D' : 'F';
                      attribs[6] = 0x00;
                      // get the time of the last write to this file
                      ctime_s( timeBuff, 30, &fileInfo.time_write );
                      // other times available on non FAT systems are create time
                      // fileInfo.time_access and fileInfo.time_write  these are -1 on FAT disks

                      printf("%-32s %s %9.ld %s",, attribs, fileInfo.size , timeBuff);
                }while(_findnext(fPtr, &fileInfo) == 0);


    main(int argc, char *argv[])


    Author Comment

    Will that work on NTFS only or any win32 system?

    Expert Comment

    This will work on any windows  system FAT or NTFS.    If you use the extra dates ( fileInfo.time_access and fileInfo.time_write ) they won't work on a FAT system.  But as written it will work on any Windows system.


    Expert Comment

    Note that you must provide a file spec (like *.* or *.ext) as well as a path or when you call the routine or it won't find any files.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    This article will show, step by step, how to integrate R code into a R Sweave document
    A short article about problems I had with the new location API and permissions in Marshmallow
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now