Viewing directory structure in C++

This is a discussion on Viewing directory structure in C++ within the C++ Programming forums, part of the General Programming Boards category; How exactly can you view the directory structure of a drive? How can you get a listing of files? Basically, ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Viewing directory structure in C++

    How exactly can you view the directory structure of a drive? How can you get a listing of files? Basically, I want to know how to write a very simple ls (unix command) or dir (windows command) but how the heck do you do it? I'm sure that there's GUI functions for it, but I want a command line way to do it (I like the command line). Anyone know?

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    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.
    Last edited by xErath; 12-05-2004 at 12:27 AM.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Will that display the contents of a given directory? What do I need to use those functions? Just include <windows.h>?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  2. Building a tree from a directory structure
    By geekoftheweek in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 03:15 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Viewing directory
    By aspza in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 08:04 AM
  5. Replies: 6
    Last Post: 07-30-2003, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21