[C] how to search a string ?

A

Alan

I want to search a string in a file, but I don't know the position, how can
I search the string and stop at that line containing that string ?

for example, I want to seach the string ".ABC" in a file, how to do ?
thx
 
M

Mac

I want to search a string in a file, but I don't know the position, how can
I search the string and stop at that line containing that string ?

for example, I want to seach the string ".ABC" in a file, how to do ?
thx

This isn't really a C question, it's an algorithm question.

It seems like the first thing you should do is read one char at a time
from the file until you find the first letter of the string. When you find
it, you can check for the second letter, and so on. If it turns out to be
a complete match, you're done. If not, you have to back up to the second
letter (this part is kind of tricky) and start the match process again.

Meanwhile, count all the newline characters so you know what line you are
on.

If you get to the end of the file, then the string must not be there. If
you find the string, you can print the line number and quit.

HTH.

Mac
--
 
M

Mike Wahler

Alan said:
I want to search a string in a file, but I don't know the position, how can
I search the string and stop at that line containing that string ?

for example, I want to seach the string ".ABC" in a file, how to do ?


Open the file with 'fopen()'.
Read each line in the file with 'fgets()'.
For each line read, search for the string with 'strstr()'
When the string found, do whatever you need with it.
Close the file with 'fclose()'.

-Mike
 
D

Default User

Mac said:
This isn't really a C question, it's an algorithm question.

I don't agree with that, the algorithm is simplistic. It's the
implementation that's the trick.
It seems like the first thing you should do is read one char at a time
from the file until you find the first letter of the string. When you find
it, you can check for the second letter, and so on. If it turns out to be
a complete match, you're done. If not, you have to back up to the second
letter (this part is kind of tricky) and start the match process again.

Good gravy, why? What's wrong with the built-in functionality for
reading lines of text and searching strings for substrings? Why would
you reinvent all that?



Brian Rodenborn
 
M

Mac

I don't agree with that, the algorithm is simplistic. It's the
implementation that's the trick.

You're in good company. ;-)
Good gravy, why? What's wrong with the built-in functionality for
reading lines of text and searching strings for substrings? Why would
you reinvent all that?

Well, I guess I mainly didn't want to go into the whole issue of how to
read in a whole line. To do it right, you have to use a lot of code, and
it is pretty clear that the OP is a total newbie at c. (No offense to the
OP intended.)

fgets() and (f)scanf() don't read in arbitrary length lines by themselves.
You have to set a maximum to do it safely.

In any case, my way involves a tricky issue when it comes to backing out
of partial matches, so I'm not sure I gained anything anyway.
Brian Rodenborn

Mac
--
 
D

Default User

Mac wrote:
Well, I guess I mainly didn't want to go into the whole issue of how to
read in a whole line. To do it right, you have to use a lot of code, and
it is pretty clear that the OP is a total newbie at c. (No offense to the
OP intended.)

More code and complication than a state machine?
fgets() and (f)scanf() don't read in arbitrary length lines by themselves.
You have to set a maximum to do it safely.

Right, but those are techniques that should be familiar to the
programmer from handling user input anyway. The tricky part with fgets()
is what to do if the line isn't complete. That's necessary when there is
no upper bound on line length in the file.

Start out with a dynamically allocated buffer, test as to whether a
complete line was read, handle it as needed. Or find one of the several
takes on the getline() problem floating around out there in
snippet-land.

In any case, my way involves a tricky issue when it comes to backing out
of partial matches, so I'm not sure I gained anything anyway.


That was my objection to the state machine approach. strstr() is already
set up for that.



Brian Rodenborn
 
N

nobody

Mike Wahler said:
Open the file with 'fopen()'.
Read each line in the file with 'fgets()'.
For each line read, search for the string with 'strstr()'

OP didn't say that that file is ASCII text file. This approach would
fail for e.g. "xx\0.ABC". This would IMHO require to open file in
binary mode, use fread(), then search "manually" (or use strstr()
"smartly", at the same time count new line "characters" (rather line
terminators? - portability issue), if OP wants to determine line number,
not only "stop at that line". Mac's approach is more workable (though I
hate reading files char at a time - I would "allocate" buffer with size
multiple of file system block size, but I digress, it's OT). And it's all
assuming ASCII encoding of file's content. I wouldn't call all this work
trivial for a newbie (no offense intended). From my limited
knowledge of Standard, I don't see any shortcut(s).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top