How to write a code in C to search a file

Dear All,
Plz give me some code that how can I search a file through a C program file may be in Present Directory or in its Sub-Directories and it should support Linux Platform. :mad:

opensolaris - link

#include <dirent.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

if \(argc != 3\)
   \{
   printf\("usage: ./Exe_Name  dir_name   file_name"\);
   exit\(0\);
   \}

if \(\(dp = opendir\(argv[1]\)\) == NULL\)
   \{
   printf\("can't open %s", argv[1]\);
   exit\(1\);
   \}
while \(\(dirp = readdir\(dp\)\) != NULL\)
   \{
    if\(!strcmp\(dirp-&gt;d_name,argv[2]\)\)
   printf\("%s\\n", dirp-&gt;d_name\);
   \}
closedir\(dp\);
exit\(0\);

}

Also consider ntfw() which is a very efficient way to do this.

See man nftw.

Or use popen to call find in a child process:

find /path/to/files -type f -exec grep -l 'searchstring' {} \;

See man popen

advanced programming inthe UNIX Environnment

Hi Manoj .. Thanks for your code .. It worked nicely for me ....

The first posted solution only works for the first level of sub-directories, not subsequent levels. To make it work recursively, you should use mcnamara's suggestion.