Solved

Search a file containing certain text....

Posted on 2001-08-08
7
199 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

813 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

12 Experts available now in Live!

Get 1:1 Help Now