It simple:
For windows applications use the FindFirstFile and FindNextFile functions
For *nix use the opendir and readdir functions
EDIT
these are in C
I changed a bit the windows example, and now uses std::cout
some small examles
WINDOWS
lists all file in c: and subdirectories
Code:
#include <string.h>
#include <iostream>
#include <windows.h>
void ListDirRec(const char *s);
int main(){
std::cout<<"Contents of c:\n";
ListDirRec("c:\\");
return 0;
}
void ListDirRec(const char *s){
char sDir[MAX_PATH]="\0", sTmp[MAX_PATH]="\0";
WIN32_FIND_DATA theFiles;
HANDLE hTheFiles;
ZeroMemory(&theFiles, sizeof(WIN32_FIND_DATA));
strcpy(sDir, s);
strcat(sDir, "*");
hTheFiles = FindFirstFile(sDir, &theFiles);
if(hTheFiles==INVALID_HANDLE_VALUE) return;
sDir[strlen(sDir)-1]=0;
do{
if( (!strcmp(".", theFiles.cFileName)) || (!strcmp("..", theFiles.cFileName)) )
continue;
if(theFiles.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
strcpy(sTmp, sDir);
strcat(sTmp, theFiles.cFileName);
strcat(sTmp, "\\");
ListDirRec(sTmp);
}else{
std::cout<<s<<theFiles.cFileName<<std::endl;
}
}while(FindNextFile(hTheFiles, &theFiles));
FindClose(hTheFiles);
return;
}
POSIX
Usage (it the same as ls or dir):
executable [directory]
Code:
#include <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[]){
char dir_name[PATH_MAX];
DIR *_dir;
struct dirent *dir_entry;
struct stat f_nfo;
int szttl=0;
if(argc==1){
getcwd(dir_name, PATH_MAX);
}else{
strcpy(dir_name,argv[1]);
}
_dir = opendir(dir_name);
printf("Contents of '%s'\n", dir_name);
while( ( dir_entry = readdir(_dir) ) != NULL){
if(stat(dir_entry->d_name, &f_nfo)){
printf("-14%s\n", dir_entry->d_name);
continue;
}
szttl+=f_nfo.st_size;
printf("%-14s ", dir_entry->d_name);
if(S_ISDIR(f_nfo.st_mode))
printf(" <DIR> ",f_nfo.st_size);
else
printf("%9d ", f_nfo.st_size);
printf("%s", ctime(&f_nfo.st_ctime));
}
printf("\tTotal: %d bytes\n", szttl);
closedir(_dir);
}
And yes to use windows API functions you need always to include windows.h, or try to find the specific header that has a specific function.