Solved

Search a file containing certain text....

Posted on 2001-08-08
7
195 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

758 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

13 Experts available now in Live!

Get 1:1 Help Now