//* Read contents of a directory (*** THIS IS NOT STANDARD C ***) *

#ifdef _MSC_VER // for MS Visual Studio compilers - to allow "classic" insecure printf and scanf #define _CRT_SECURE_NO_WARNINGS #endif #include <stdio.h> #include <dirent.h> // UNix, Linux, BSD, MacOSX (which is kind of BSD) int main() { char fileName[80]; DIR* directory; struct dirent* dirEntry; printf("Read the directory content of: "); scanf(" %79[^\n]s\n", fileName); directory = opendir(fileName); for(;;) { dirEntry=readdir(directory); if (dirEntry==0) break; // all entires have been read if ( dirEntry->d_type==DT_DIR ) { printf("D %s\n", DirEntry->d_name); } else if ( DirEntry->d_type==DT_REG ) { printf("F %s\n", DirEntry->d_name); } else if ( DirEntry->d_type==DT_LNK ) { printf("L %s\n", DirEntry->d_name); } else if ( DirEntry->d_type==DT_FIFO ) { printf("P %s\n", DirEntry->d_name); } else if ( DirEntry->d_type==DT_BLK ) { printf("B %s\n", DirEntry->d_name); } else if ( DirEntry->d_type==DT_CHR ) { printf("C %s\n", DirEntry->d_name); } else if ( DirEntry->d_type==DT_SOCK) { printf("S %s\n", DirEntry->d_name); } else if ( DirEntry->d_type==DT_UNKNOWN) { printf("? %s\n", DirEntry->d_name); } else { printf("! %s has file type error\n", DirEntry->d_name); } } closedir(directory); return(0); }

//* Oh! I forgot to tell you. This feature is OS specific. This program will run on *almost* all but Windows or DOS *