By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

C++: Input pattern and find in text file

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
kittykat wrote:
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).
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #2

P: n/a
Thank you so much! I will look into that.
Jul 22 '05 #3

P: n/a
In message <11**********************@c13g2000cwb.googlegroups .com>,
Dietmar Kuehl <di***********@yahoo.com> writes
kittykat wrote:
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).


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.

--
Richard Herring
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.