• C

Search a file containing certain text....

How do I search a directory for all files or files like *.txt or .t?t for the occurrence of a searchstring like "Hello"?

Who is Participating?
TriskelionConnect With a Mentor Commented:
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?
Do you know 'regular expressions'?
Do you know perl?
Do you want to do this in C?
roglanAuthor Commented:
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

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

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

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!!!!!!!!
roglanAuthor Commented:
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?
I don't know.
You'll have to ask the admin if that's possible.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.