![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to write a wrapper c code to return uid using getuid() function | pwd | High Level Programming | 4 | 05-13-2008 07:01 AM |
| search change write | trilicno | UNIX for Dummies Questions & Answers | 3 | 03-19-2008 06:30 AM |
| search change write | trilicno | UNIX for Dummies Questions & Answers | 1 | 03-18-2008 02:34 AM |
| Write the source code for messenger | !_30 | High Level Programming | 9 | 11-02-2006 11:34 AM |
| need help to write perl code | getdpg | Shell Programming and Scripting | 0 | 09-20-2006 06:24 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
#3
|
|||
|
|||
|
C Code to search a file in directory you can modify it for a perticular path
#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->d_name,argv[2])) printf("%s\n", dirp->d_name); } closedir(dp); exit(0); } |
|
#4
|
|||
|
|||
|
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: Code:
find /path/to/files -type f -exec grep -l 'searchstring' {} \;
|
|
#5
|
|||
|
|||
|
advanced programming inthe UNIX Environnment
|
|
#6
|
|||
|
|||
|
Hi Manoj .. Thanks for your code .. It worked nicely for me ....
|
|
#7
|
|||
|
|||
|
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.
|
|||
| Google The UNIX and Linux Forums |