Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

BinarySearch on TextFile

hey thanks 4 ur reply i see what u mean and the records are sortedby the index number for example
0 xxxxx
1 xxxxx
2 xxxxx
3 xxxxx
4 xxxxx
5 xxxxx
6 xxxxx
7 xxxxx
8 xxxxx
Well the thing is i know what binary search is but i dont know how to apply in vb.net and on a file so if u can help ill appreciate it thanks a lot.If u can give me an example or a code thank u.
its is sorted
sam
[433 byte] By [dicky18] at [2007-11-11 6:54:37]
# 1 Re: BinarySearch on TextFile
' Open text file
Dim fs As FileStream
fs = File.OpenRead("c:\temp\test.txt")

' Read contents into string
Dim sr As New StreamReader(fs)
Dim text As String = sr.ReadToEnd()
sr.Close()
fs.Close()

' Split string into array of lines
Dim lines() As String = Split(text, vbCrLf)
' Search for an item
Dim item As Integer = Array.BinarySearch(lines, "4 xxxxx")
If item > 0 Then
Debug.WriteLine("Found: {0}", lines(item))
Else
Debug.WriteLine("4 xxxxx not found.")
End If
Phil Weber at 2007-11-11 21:49:53 >
# 2 Re: BinarySearch on TextFile
first of all thank u Sir very much for ur reply i wasve helpful and i hope im not disturbing you but i have another question and it might be difficult anyways ill try
so have two files oneis indexfile and the other datafile

indefile
0,4735487
1,4735488
2,4735489
3,4735490

datafile
3,4735490,sam,canada
1,4735488,dave,england
2,4735489,HArry,FRance
0,4735487,tom,US

well i have to do binary search on the index file for id 4735488 for example
well what i need is to get the index of this id which is 1 here and search the data file for this index and display its data in a listbox here its (1,4735488,dave,england)
i know its complicated but i really need to do this and its helpful for indexing and i hope im not disturbing or wasting r time thanks again forur reply thank you and by the way nice website..
dicky18 at 2007-11-11 21:50:51 >