• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4367
  • Last Modified:

ANSI C - list directory contents

Hi

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

Thanks

-Mike
0
nerdmike
Asked:
nerdmike
  • 3
  • 3
  • 2
  • +1
1 Solution
 
nerdmikeAuthor Commented:
Those were all unix specific solutions

I need ANSI C *ONLY*
0
 
sunnycoderCommented:
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

Cheers!
sunnycoder
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nerdmikeAuthor Commented:
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
0
 
sunnycoderCommented:
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

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20909231.html

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20761334.html

Cheers!
0
 
MMDaveCommented:
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:\\*.*");
}

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now