The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-19-2007
Registered User
 

Join Date: Mar 2007
Posts: 25
Unhappy 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.
Reply With Quote
Forum Sponsor
  #2  
Old 03-19-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
opensolaris - link
Reply With Quote
  #3  
Old 03-27-2007
Registered User
 

Join Date: Mar 2007
Posts: 16
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);
}
Reply With Quote
  #4  
Old 03-27-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
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' {} \;
See man popen
Reply With Quote
  #5  
Old 04-18-2007
Registered User
 

Join Date: Apr 2007
Posts: 14
advanced programming inthe UNIX Environnment
Reply With Quote
  #6  
Old 08-04-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore
Posts: 7
Thumbs up Thank you

Hi Manoj .. Thanks for your code .. It worked nicely for me ....
Reply With Quote
  #7  
Old 08-04-2008
Registered User
 

Join Date: Aug 2008
Location: Portugal
Posts: 212
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0