473,785 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string comparision in a file

Hi,
I have a requirement in which I need to search a string in a file
stored on a harddisk. String which needs to be searched would be
stored in a file with delimiter as new line character. some of the
ways of doing this are

1. Read each line and do string compare immediately.
2. Read each line, add each string into a stl vector and search for
the given string using stl bineary_search algorithm.

It would be helpful if someone could give me comparison of which would
be the best method among the above mentioned ways of handling
searching. I would also welcome any other better of handling searching
string in a file.

Thanks,
Rakesh.
Jul 22 '05 #1
6 1853

"rakesh" <ra*****@yahoo. com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
Hi,
I have a requirement in which I need to search a string in a file
stored on a harddisk. String which needs to be searched would be
stored in a file with delimiter as new line character. some of the
ways of doing this are

1. Read each line and do string compare immediately.
2. Read each line, add each string into a stl vector and search for
the given string using stl bineary_search algorithm.

It would be helpful if someone could give me comparison of which would
be the best method among the above mentioned ways of handling
searching. I would also welcome any other better of handling searching
string in a file.


The first way is better in every respect. It is quicker, it uses less memory
and it is simpler to code. Sometimes the simplest way is the best.

john
Jul 22 '05 #2
In message <2m************ @uni-berlin.de>, John Harrison
<jo************ *@hotmail.com> writes

"rakesh" <ra*****@yahoo. com> wrote in message
news:d9******* *************** ****@posting.go ogle.com...
Hi,
I have a requirement in which I need to search a string in a file
stored on a harddisk. String which needs to be searched would be
stored in a file with delimiter as new line character. some of the
ways of doing this are

1. Read each line and do string compare immediately.
2. Read each line, add each string into a stl vector and search for
the given string using stl bineary_search algorithm.

It would be helpful if someone could give me comparison of which would
be the best method among the above mentioned ways of handling
searching. I would also welcome any other better of handling searching
string in a file.

The first way is better in every respect. It is quicker, it uses less memory
and it is simpler to code.


Also the second way is wrong. binary_search only works on sorted data
:-(
Sometimes the simplest way is the best.

However, I wonder whether Rakesh is really searching for a string which
matches an entire line in the file, or only a part of the line?

If the latter, it might be worth his while to research more
sophisticated string search algorithms. Boyer-Moore is one, but there
are many others, and they be much more efficient than a naive linear
search.

--
Richard Herring
Jul 22 '05 #3
If you need to search your string nearly hundred times within a minute,
and -
your search will be conducted in a file which contains thousands of
strings(lines) , and -
you don't have to worry about your memory capacity, then -
you'd better use the second method.

The reason for it, from my point of view, is :
STL vector/list is implemented with a much better algorithm for searching an
element, very good efficiency.
It helps save time, but it also causes bigger memory consumption.

For a small job, you can just choose the first method, as mentioned by John.

pengfei
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2m******** ****@uni-berlin.de...

"rakesh" <ra*****@yahoo. com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
Hi,
I have a requirement in which I need to search a string in a file
stored on a harddisk. String which needs to be searched would be
stored in a file with delimiter as new line character. some of the
ways of doing this are

1. Read each line and do string compare immediately.
2. Read each line, add each string into a stl vector and search for
the given string using stl bineary_search algorithm.

It would be helpful if someone could give me comparison of which would
be the best method among the above mentioned ways of handling
searching. I would also welcome any other better of handling searching
string in a file.

The first way is better in every respect. It is quicker, it uses less

memory and it is simpler to code. Sometimes the simplest way is the best.

john

Jul 22 '05 #4
rakesh wrote:
Hi,
I have a requirement in which I need to search a string in a file
stored on a harddisk. String which needs to be searched would be
stored in a file with delimiter as new line character. some of the
ways of doing this are

1. Read each line and do string compare immediately.
2. Read each line, add each string into a stl vector and search for
the given string using stl bineary_search algorithm.

It would be helpful if someone could give me comparison of which would
be the best method among the above mentioned ways of handling
searching. I would also welcome any other better of handling searching
string in a file.

Thanks,
Rakesh.


If your system supports muli-tasking (multi-threading or whatever),
you could have one task reading lines into a buffer while another
task searches a given line.

This would assume that reading a line takes longer than searching it
and there is considerable overhead time when reading a line (such as
hard-drive start-up time, seeking time, etc.). Although these
timings usually only appear when the number of lines to read in
is huge; or the I/O overhead times are really slow.

Hmmm, why do things simple when you can have some fun making them
more complex? ;-)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5
ra*****@yahoo.c om (rakesh) wrote in message news:<d9******* *************** ****@posting.go ogle.com>...
Hi,
I have a requirement in which I need to search a string in a file
stored on a harddisk. String which needs to be searched would be
stored in a file with delimiter as new line character. some of the
ways of doing this are

1. Read each line and do string compare immediately.
2. Read each line, add each string into a stl vector and search for
the given string using stl bineary_search algorithm.


It depends on where you expect to find the target string (on average),
and how much of each string you need to search before you can
determine that it doesn't match. It also depends on how large the file
is since you have to store it in-core with the second approach. If the
file is static, you can do some optimizations such as build an
auxiliary index (file), use a database, etc.

/david
Jul 22 '05 #6
ra*****@yahoo.c om (rakesh) wrote in message news:<d9******* *************** ****@posting.go ogle.com>...
Hi,
I have a requirement in which I need to search a string in a file
stored on a harddisk. String which needs to be searched would be
stored in a file with delimiter as new line character. some of the
ways of doing this are

1. Read each line and do string compare immediately.
This is basically an O(N) algorithm.
2. Read each line, add each string into a stl vector and search for
the given string using stl bineary_search algorithm.


For binary_search to be meaningful, the items in the vector would
first have to be sorted. The sort (by itself) is slower than the
first search.

The second method makes sense if and only if you can read and sort the
data once, and then do multiple searches on the data. In that case,
the initial read/sort step is still O(N * log N), but each subsequent
search is O(log N), whereas in the first case, each search is O(N).

For string searching, I prefer Sunday's variant of
Boyer-Moore-Horspool string searching. Assuming the string you're
searching for is long and low in repitition, it can be quite fast.
OTOH, this is often overkill -- in a reasonably typical situation, the
real bottleneck will be in the I/O.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1642
by: kd | last post by:
Hi All, How to perform case-insensitive comparision of strings? Would there be some kind of an indicator, which when set to true, would allow case-insenitive comparision of strings using if/select case, etc; after performing the case-insenisitive compare, the indicator can be reset to allow the conventional comparision of strings? kd
3
1121
by: krallabandi | last post by:
Hi, I have a requirement to compare 2 ASCII text files and show the results in an aspx form. The comparision algorithnm, out put should be similar to VSS (differences between 2 files). I really appreciate if you can tell me the best way of doing this and what web controls suits this requirement.
30
3321
by: Steve Edwards | last post by:
Hi, I'm re-writing some code that had relied on some platform/third-party dependent utility functions, as I want to make it more portable. Is there a standard C/C++/stl routine for changing an stl string to all lowercase? (I know how to do it manually, but in the interests of portability...) Thanks Steve
3
2532
by: priyanka | last post by:
Hi there, I am trying to compare two strings. But I am not able to do so. I use : if(function_name == function_label){ but this does not work. I have written down my code:
8
5744
by: abhi147 | last post by:
Hi all , I need to convert a 32 bit string containing hex digits like "76d408cedd600bb578bc0256a751cea2" into it's corresponding 16bit ascii value like "vÎÝ` µx¼V§Q΢" . Now the presence of Hex digits like "0a" ,"00" , "0b" whose ascii value is New line , NULL , Tab respectively brings the rest of the string to new line or put a tab in between .
9
5043
by: subramanian100in | last post by:
Suppose we have char *a = "test message" ; Consider the comparison if (a == "string") ..... Here "string" is an array of characters. So shouldn't the compiler
1
3333
kirubagari
by: kirubagari | last post by:
I Would Like Ask Ques On How To Open Bonding.bin File In C++ Or In Visual Basic 6.actually The Data In The Binary File Have Been Corrupted And I Would Like To Know The Method How To Open The File That Contain In The Floopy Disk And Do Comparision So That The Corruted Data Can Be Changed Automaticaly.i Realy Need Ur Help Guys
7
2717
pureenhanoi
by: pureenhanoi | last post by:
Hi bro! Can anyone tell me, how to compare two strings like Operating System compare file names. Example: if i have two string "T*.DOC" and "TIET1.DOC". The comparision operator must return TRUE values when compare them
1
1166
by: madhurathod | last post by:
Hi guys, Am novice of perl. i want the code for wave file comparision so can anyone please do this job for me
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.