Hello,

i have a function that starts searching for all files that match the given file name and Recursively traverses itself into sub directories and does the same thing and when a file is found i should output its path starting from the first directory that was given to the function. my problem is that i keep having partial paths .

Here is the part of code where everything goes wrong

buffer = (char*) malloc (strlen(dirname)+1);
	
	while (  (dir = readdir( d )) ) 
	{
		if (strcmp(dir->d_name,".") == 0 || strcmp(dir->d_name,"..") == 0)
			continue;
		
		
		if (buffer==NULL) 
			exit (1);
	
		strcpy(buffer,dirname);
		num=strlen(buffer);
		buffer[  num]='\0';
	
	
		stat(dir->d_name,&info);
		if(S_ISDIR(info.st_mode) )
		{ 
		
 			
			 buffer = (char*) realloc (buffer,(strlen(buffer)+1));
			 strcat(buffer,"/");
			 strcat(buffer,dir->d_name);
			 num=strlen(buffer);
			 buffer[  num]='\0';
			 
			 tmp = (char*) malloc (strlen(buffer)+1);		
			 strcpy(tmp,buffer);						

			search(fname, dir->d_name, min ,max);
			chdir("..");
 strcpy(tmp,buffer);	

		
		}

		
		

			else if((pch = strstr (dir->d_name,fname)) != NULL )
			{
				
				
				 buffer = (char*) realloc (buffer,(strlen(dir->d_name)+strlen(buffer)));
				strcat(buffer,"/");
				 strcat(buffer,dir->d_name);
				 num=strlen(buffer);
				 buffer[  num]='\0';

				printf("%s\n", buffer);
	
			}
		

	}free(buffer);
	closedir(d);
}

Edited 6 Years Ago by chiko987: n/a

This article has been dead for over six months. Start a new discussion instead.