Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching for file in directory

Status
Not open for further replies.

CaptRage61

Programmer
Feb 17, 2005
50
US
I want to create a method to search for a given file(or directory) in a given directory, here is what I came up with but it always says that it was not found even if the file exists.

Code:
void search ( char *filename , char *dirname ){
  struct dirent *dirPointer;
  DIR *d;
  d = opendir( dirname );
  
//had code to print error message but took out to save space

   while ( (dirPointer=readdir(d)) != NULL ) {
     if ( dirPointer->d_name == filename ) {
         // print absolute path of file
         printf("File located\n");
       }
     else printf("%s Not found in %s\n",filename , dirname );
}


}

Any ideas on how to get this to work
 
This is the same thing we were trying to tell you in the other thread. C strings are POINTERS, so comparison with an == tells you weather they are pointing to the same place in memory not if the values of what is being pointed at are the same.

You need to use strcmp...
 
This is the same thing we were trying to tell you in the other thread. C strings are POINTERS, so comparison with an == tells you weather they are pointing to the same place in memory not if the values of what is being pointed at are the same.

You need to use strcmp...

Code:
void search ( char *filename , char *dirname ){
  struct dirent *dirPointer;
  DIR *d;
  d = opendir( dirname );
  
//had code to print error message but took out to save space

   while ( (dirPointer=readdir(d)) != NULL ) {
     if ( [red]strcmp( dirPointer->d_name, filename) == 0[/red]) {
         // print absolute path of file
         printf("File located\n");
       }
     else printf("%s Not found in %s\n",filename , dirname );
}


}

strcmp takes two char * (let's call them a and b, respectivly).
If a < b then strcmp(a,b) < 0, if a == b then strcmp(a,b) == 0, and if a > b then strcmp(a,b) > 0. Typically, strcmp returns -1, 0 or 1 ... but don't trust this, as this varies from implimentation to implementation... always use < 0, == 0, or > 0.
 
Ok, that worked, now I only have 2 questions left, how do I print the absolute path of the filename and how do I get rid of this stupid segmentation fault when there are moer than 2 arguments????


Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>


void search ( char *filename , char *dirname ){
  int f = 0;
  struct dirent *dirPointer;
  DIR *d;
  d = opendir( dirname );


   if ( d == NULL ) {
        fprintf( stderr, "%s %d: opendir() failed (%s)\n",
            dirname, errno, strerror( errno ));
        exit( EXIT_FAILURE );
    }
   while ( (dirPointer=readdir(d)) != NULL ) {
     if (strcmp( dirPointer->d_name, filename) == 0 ) {
         // print absolute path of file
         printf("File located\n");
         f = 1;
       }

}
if ( f == 0 ) {
printf("File not found\n");
}
closedir ( d );
printf("\n");
}

int main( int argc, char *argv[] ) {
    int x = argc;
    int count;
    printf("Number of args: %d\n",x);
     if ( x == 2 ){
      //search for file or dir in current dir
      printf("Searching\n");
      search( argv[1] , "." );
    }
     if ( x > 2 )  {
        //first n-1 are files to search for
        //n is the folder to search

        for ( count = 2 ; count < argc ; count++ ) {
            search( argv[count],argv[argc]);

}
    }
     if ( x == 1 ) {
        printf("Not enough input\n");
     }

return 0;
}
 
argv[argc]
argc is a count: 1, 2, 3, 4.
argv is an array with a zero based index: 0, 1, 2, 3.
argv[argc] will always give you a segfault if your lucky. If your not, the memory byte just after argv will belong to your program, and will have a value that will make your program "work" oddly.

You need to start by getting the present working directory, and when you recurse into the other directories you need to append the new directories to the end.

[plug=shameless]
[/plug]
 
Thanks, after taking a break and looking back at it I finally saw that.
 
Some addition (to jstreich):
From C++ Standard (as far as I know, the same words are in C Std):
The value of argv[argc] shall be 0.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top