Link to home
Start Free TrialLog in
Avatar of nerdmike
nerdmike

asked on

ANSI C - list directory contents

Hi

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

Thanks

-Mike
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of nerdmike
nerdmike

ASKER

Those were all unix specific solutions

I need ANSI C *ONLY*
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sucks

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

An example using the win32 api would be nice.

-mike
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

https://www.experts-exchange.com/questions/20909231/How-can-I-list-all-File-Name-in-specific-path.html

https://www.experts-exchange.com/questions/20761334/Directory-browsing-with-C-in-VS-on-Windows.html

Cheers!
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

-david


#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)
            return;
      else
      {
            do{

                  // 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",fileInfo.name, attribs, fileInfo.size , timeBuff);
            }while(_findnext(fPtr, &fileInfo) == 0);
            _findclose(fPtr);
      }

}

main(int argc, char *argv[])
{
      listDir("c:\\*.*");
}

Will that work on NTFS only or any win32 system?
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.

-dave
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.