Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <dir.h>
#include <string.h>
#include <alloc.h>
#include <stdlib.h>

struct btreenode
{
    struct btreenode *leftchild ;
    char data[13] ; /* file name */
    char *loc ; /* location of filename */
    struct btreenode *rightchild ;
} *bt = NULL ;

void disktree( ) ;
int insert ( struct btreenode  **, char*, char* ) ;

void main( )
{
    char current_dir[32] ;

    clrscr( ) ;

    getcwd ( current_dir, 32 ) ;
    chdir ( "\\" ) ;
    disktree( ) ;
    chdir ( current_dir ) ;

    getch( ) ;
}

void disktree( )
{
    struct ffblk file ;
    int flag ;
    char loc[80] ;

    getcwd ( loc, 80 ) ;
    flag =  findfirst ( "*.*", &file, FA_NORMAL | FA_RDONLY | FA_HIDDEN |
		    FA_SYSTEM | FA_LABEL | FA_DIREC | FA_ARCH ) ;

    while ( flag == 0 )
    {
	if ( file.ff_name[0] != '.' )
	{
	    if ( file.ff_attrib == FA_DIREC && file.ff_fsize == 0 )
	    {
		chdir ( file.ff_name ) ;
		disktree( ) ;
		chdir ( loc ) ;
	    }
	    else
		insert ( &bt, loc, file.ff_name ) ;
	}
	flag = findnext ( &file ) ;
    }
}

/* inserts a new node in a binary search tree */
int insert ( struct btreenode  **sr, char* l, char* f )
{
    char *p ;
    int flag ;

    if ( *sr == NULL )
    {
	*sr = ( struct btreenode * ) malloc ( sizeof ( struct btreenode ) ) ;

	if ( *sr == NULL )
	{
	    printf ( "\nOut of memory." ) ;
	    exit(1);
	}

	( *sr ) -> leftchild = NULL ;
	( *sr ) -> rightchild = NULL ;
	strcpy ( ( *sr ) -> data, f ) ;
	p = ( char * ) malloc ( ( strlen ( l ) + 1 ) ) ;

	if ( p == NULL )
	{
	    printf ( "\nOut of memory." ) ;
	    exit(1);
	}

	strcpy ( p, l ) ;
	( *sr ) -> loc = p ;
    }
    else
    {
	flag = strcmp ( ( *sr ) -> data, f ) ;

	if ( flag == 0 )
	{
	    printf ( "org: %s", ( *sr ) -> loc ) ;

	    if ( strlen ( ( *sr ) -> loc ) > 4 )
	    {
		printf ( "\\" ) ;
		printf ( "%s\n", ( *sr ) -> data ) ;
		printf ("dup: %s", l ) ;
	    }
	    if ( strlen ( l ) > 4 )
	    {
		printf ( "\\" ) ;
		printf ( "%s\n\n", f ) ;
	    }
	}
	else if (flag < 0 )
        {
	    insert ( &( ( *sr ) -> leftchild ), l, f ) ;
        }
	else
	    insert ( &( ( *sr ) -> rightchild ), l, f ) ;
    }
    return 0;
}
Posted
Updated 1-Nov-14 19:46pm
v2
Comments
George Jonsson 2-Nov-14 1:49am    
Removed the urgency. It is only urgent to you and unless you want to pay for the help, it is not urgent for anyone else.
Also try to format the code if you want anyone to bother to read it through.
(I did it for you this time)

1 solution

How long are filenames? from your code:
<br />
  char data[13] ; /* file name */<br />
  char current_dir[32] ;<br />
  char loc[80] ; <br />

Also, if you have two conditions around any output. Are they correct?
if ( strlen ( ( *sr ) -> loc ) > 4 )<br />
if ( strlen ( l ) > 4 )<br />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900