?
Solved

Search a file containing certain text....

Posted on 2001-08-08
7
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

752 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