Directory Traversing in C using Tree Data Structure

Binary Decision Diagram (BDD) and Truth Table

Truth Table and Decision Tree Representations of a Boolean Function. A dashed (solid) tree branch denotes the case where the decision variable is 0 (1)

This is a C Program to implement tree data structure. It uses current working directory as the root and traverse all files inside the directory and prints them on the screen.

You can use any C/C++ compilers to compile and run this program however, it is tested using Turbo C++ Compiler only.

#include <stdio.h>
#include <dos.h>
#include <dir.h>
#include <conio.h>
#include <cstring.h>

char directory_name[128]; //  maximum lenght is 128  as far as i had seen in
//  win9x, but xp goes upto approx==231. dir length

char * view[] = {
  "-----",
  "-",
  "   -----",
  "   ",
  "    -----"

};

void TREE_TRAVERSE(char * );
int counter, sw = 0;
char current_working_directory[128];

void main() {
  clrscr();
  getcwd(current_working_directory, 128); // preserve curr. dir..
  chdir("\\"); //to root , this is important

  TREE_TRAVERSE("*.*"); // traverl...
  chdir(current_working_directory); // store to present working directory...
}

void TREE_TRAVERSE(char * pointer) {
  struct ffblk file;
  int flag, num = 0, i, j;
  static int deep_inside = 0;
  flag = findfirst(pointer, & file, FA_DIREC); // find directory and not filez...

  while (flag == 0) // loop until you are out
  {
    if ((file.ff_attrib & FA_DIREC) == FA_DIREC && file.ff_name[0] != '.') {
      deep_inside++; // go into directory...
      directory_name[0] = '\0';

      if (deep_inside == 1) //if on root put "-"
        strcat(directory_name, view[0]);
      else {
        strcat(directory_name, view[1]); //else decorate
        i = 2;
        while (i < deep_inside) // put space upto...
        {
          strcat(directory_name, view[3]); //3 for space
          i++;
          //
          printf("%d", i);
        }
        if (i == 2) {
          strcat(directory_name, view[4]);
        }
        //
        if (i == 3) {
          strcat(directory_name, view[1]);
          strcat(directory_name, view[2]);

        } //up dated
        else {
          strcat(directory_name, view[2]);
        }
      }
      strcat(directory_name, file.ff_name);
      printf("%s", directory_name); // print dir name 
      //if out of dir's then goto new line 
      if (num != deep_inside)
        
        printf("\n"); // 
      getch();
      chdir(file.ff_name);
      file.ff_name[0] = '\0';
      TREE_TRAVERSE(pointer);
    } //e-o-if 
    flag = findnext( & file);
  } // e-o-while 
  if (deep_inside--> 0)
    chdir("..");
}
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post