473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++: Input pattern and find in text file

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
3 5092
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
Thank you so much! I will look into that.
Jul 22 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christopher R. Barry | last post by:
I need to search and replace multiple words in one pass of an input stream or string. For example, given the input: "The quick brown fox jumped over the lazy dog's back" and given the...
5
by: Gregg | last post by:
Hello all, I have been banging my head over a problem that I am having reading a comma seperated file (CSV) that can contain from 1 to 10,000 records. My code snipit is as follows: **Start...
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
4
by: Jéjé | last post by:
Hi, I have a file which contain 1 pair of values by line like: Name1=Value1 = I nned to store these pair of values in a sortedlist. So the result expected for the 2 samples lines is: Key ...
27
by: code_wrong | last post by:
Visual Basic (not dot net) what is the best way to check the User has entered an integer into an InputBox? isNumeric() checks for a numeric value .. but does not notify of numbers with decimal...
2
by: Holger | last post by:
I needed a tool for extracting patches from CVS based on the log messages. I.e. we mark our fixes and features with a "Bugdb XYZ" And sometimes you need to move a fix/feature to another branch or...
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
2
by: vijaykumardahiya | last post by:
Hello Sir, I have a simple Issue but It is not resolve by me i.e input parameter are not store in Ms-Access. I store the input parameter through Standard Action <jsp:useBean>. jsp:useBean call a...
2
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
I have a small problem with the input redirection. to search for a particular pattern in all files i have used this command find $dir -type f -print | xargs grep -E -n -w <pattern> | tee my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.