473,387 Members | 1,621 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.

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 1821

"rakesh" <ra*****@yahoo.com> wrote in message
news:d9**************************@posting.google.c om...
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.google. 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.google.c om...
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.learn.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.com (rakesh) wrote in message news:<d9**************************@posting.google. 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.com (rakesh) wrote in message news:<d9**************************@posting.google. 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
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...
3
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...
30
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...
3
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
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...
9
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
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...
7
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...
1
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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...
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.