Solved

Search a file containing certain text....

Posted on 2001-08-08
7
198 Views
Last Modified: 2010-05-18
How do I search a directory for all files or files like *.txt or .t?t for the occurrence of a searchstring like "Hello"?

Roger
0
Comment
Question by:roglan
7 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 100 total points
ID: 6365128
You can search the directory with (something like) findfirst() and continue with something like findnext().

You then can open each file with fopen() and use fgets() to get the data one line at a time.

Use strtok() to parse through each line and determine if the string is there.

What compiler and OS are you using?
0
 

Expert Comment

by:digized
ID: 6365179
Do you know 'regular expressions'?
Do you know perl?
Do you want to do this in C?
0
 

Author Comment

by:roglan
ID: 6366185
I'm using a Borland Compiler 5.5.

I've had a look at the findfirst()/findnext methods, and thinks they look good, I'll come back when I've tried them out...thanks.

And yes: I want to do this in C, I do know a bit of Perl, but I'd prefferd to have this work in more or less standard C

Roger
0
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 
LVL 4

Expert Comment

by:newmang
ID: 6366327
strtok will only be useful if the search string is one word. I would suggest that you use the strstr() function instead.

This function is case-sensitive, if you wish to do a case insensitive search you could convert your seach string to uppercase using the strupr() function and then convert each line read from the file to uppercase before running the strstr() function (you may wish to copy the line into a second buffer to convert to uppercase so you have an unchanged copy to print out or you could keep track of your position in the file using the ftell() function then re-read any lines that you want to print out using the fseek() function).

Cheers - Gavin
0
 

Expert Comment

by:parag_t
ID: 6366800
Hello

You can use findfirst() & findnext functions.
You can have prototype of these functions defined in Turbo C help. Actually the solution which u want can be found in findfirst & findnext example given in that help. The is self-explanatory.
wish u all the best!!!!!!!!
bye
Parag
0
 

Author Comment

by:roglan
ID: 6375450
I've already got the answers I needed from newmang and Triskelion - Thanks guys!

Is there any way I can divide the points between you?
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6375498
I don't know.
You'll have to ask the admin if that's possible.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now