C++: Input pattern and find in text file

K

kittykat

Hi,
I was wondering if you could help me. I am writing a program in C++, and
the problem is, i have very limited experience in this language.
I would like my user to enter a specific pattern, and I want my program to
search a text file for this pattern, and let the user know if this pattern
exists or not.

So far, i have figured out how to make my prgram read the text file, but
i'm not sure how to take the information the user inserts and compare it
with the data in the text file.

It would be great if you could point me in the right direction, and give
me a few tips on how i should go about implementing this.

Thanx so much in advance.
 
D

Dietmar Kuehl

kittykat said:
I would like my user to enter a specific pattern, and I want my program to
search a text file for this pattern, and let the user know if this pattern
exists or not.

I assume your "pattern" is more complex than just a string which is
to be sought in the text file (otherwise the solution is trivially
to read the textfile into a 'std::string' or a 'std::vector<char>'
and use an appropriate 'find()' method or algorithm). That is, I
assume your pattern is more like a regular expression. In this case
your best option is probably to get the RegEx library from Boost
(<http://www.boost.org/>) and use it: a library similar to this will
be part of Library TR1 and is thus likely to become part of the next
revision of the C++ standard.

If you really want to program everything yourself, you should
investigate "finite state machines" and pattern matching: effectively,
you use a finite state machine to efficiently match a certain class
of patterns (essentially those which can't match nested parenthesis).
 
R

Richard Herring

In message said:
I assume your "pattern" is more complex than just a string which is
to be sought in the text file (otherwise the solution is trivially
to read the textfile into a 'std::string' or a 'std::vector<char>'
and use an appropriate 'find()' method or algorithm).

But even then, if the file is large the "appropriate" algorithm is
probably *not* one of the variants of std::find() or string::find(),
which are likely to be naive O(n*m) methods.

Google searching on "string search algorithm" will yield some useful
faster algorithms - Boyer-Moore, Knuth-Morris-Pratt, and no doubt many
others.
 

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
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top