How to write a code in C to search a file


 
Thread Tools Search this Thread
Top Forums Programming How to write a code in C to search a file
# 1  
Old 03-19-2007
Data 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. Smilie
# 2  
Old 03-19-2007
# 3  
Old 03-27-2007
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  
Old 03-27-2007
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
# 5  
Old 04-18-2007
advanced programming inthe UNIX Environnment
# 6  
Old 08-04-2008
MySQL Thank you

Hi Manoj .. Thanks for your code .. It worked nicely for me ....
# 7  
Old 08-04-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search text beween tags and write to file using awk

Hi Friends, I have a very big text file, that has code for multiple functions. I have scan through the file and write each function in seperate file. All functions starts with BEGIN DSFNC Identifier "ABCDDataValidationfnc" and ends with END DSFNC I need create a file(using identifier)... (2 Replies)
Discussion started by: anandapani
2 Replies

2. Shell Programming and Scripting

Search pattern and write line into another file

Hi, I have a file which contains the below details.. My requirement is to fetch all the lines which are starting with "ABC_XY_" into 1 file and rest of the lines (not starting with "ABC_XY_") into another file. Could you please help with what command needs to be used? file1.txt ----------... (12 Replies)
Discussion started by: satyaatcgi
12 Replies

3. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

4. Shell Programming and Scripting

"Search for files in a file and write these files to another file which sould be in some other direc

Hi , Need to shell script to extracts files names and write those to another file in different directory. input file is inputfile.txt abc|1|bcd.dat 123 david123 123 rudy2345 124 tinku5634 abc|1|def.dat 123 jevid123 123 qwer2345 124 ghjlk5634 abc|1|pqr.txt 123 vbjnnjh435 123 jggdy876... (1 Reply)
Discussion started by: dssyadav
1 Replies

5. Shell Programming and Scripting

search any user files with write permission

Guys, i wanna get any user files with write permission (on user or group permission) for review but i confuse with -perm parameter. any body can help me to explain what is that mean? thank's (1 Reply)
Discussion started by: michlix
1 Replies

6. Shell Programming and Scripting

search 3 file and write to 4th file (a bit complex)

hi buddies; rollbackip.txt:10.14.3.65 2 10.14.3.65 3 ... lookup.txt: ... 10.14.3.65 2 10.14.5.55 1 55 10.14.6.66 1 66 10.14.3.65 3 10.14.7.77 3 77 10.14.8.88 2 88 10.14.9.99 4 99 ... ip-port.txt ... port111 3 10.14.5.55 57 port111 2 10.14.5.55 51 port111 1 10.14.5.55 59 ->... (7 Replies)
Discussion started by: gc_sw
7 Replies

7. Shell Programming and Scripting

write shell script to search file in folder

hi .. i have a problem how to search file with date and version number(ms_2.0_dd/mm/yy_24)in folder.Here 24 is version number and compare the this file to other file which is in another folder and if does not match then copy this file to respective folder.Also copy different files in different... (1 Reply)
Discussion started by: shubhig15
1 Replies

8. Programming

Code to write the heap into a file

Hi everybody, i am doing a project on checkpoints, where in i need to write the heap area into a file. I have got a code which i need to analyze, also asked in the forum under solaris https://www.unix.com/solaris/135483-libckpt.html The heap is implemented as a link list given by the structure... (2 Replies)
Discussion started by: holla4ni
2 Replies

9. UNIX for Dummies Questions & Answers

search change write

i="1" while do k=`expr $i + 1` sed -e 's/"$i"/"$k"/' < somefile1.txt >> somefile2.txt i=`expr $i + 1` done ive been trying to : 1 from a file containing numbers in a row divided with correct number of spaces to find the corresponding one 2 change the value 3 and append... (3 Replies)
Discussion started by: trilicno
3 Replies

10. UNIX for Dummies Questions & Answers

search change write

i="1" while do k=`expr $i + 1` sed -e 's/"$i"/"$k"/' < somefile1.txt >> somefile2.txt i=`expr $i + 1` done ive been trying to : 1 from a file containing numbers in a row divided with correct number of 2 spaces to find the corresponding one 2 change the value 3 and append... (1 Reply)
Discussion started by: trilicno
1 Replies
Login or Register to Ask a Question