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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #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(".."); } |