Re: database theory
>Yes, but how would you know to seek the third one? Let me think. Well,
>here. How fast is this?
>sequence last_name
>last_name = ""
>while last_name != "Jordan" -- the name I'm trying to retrieve
> counter = 1
> seek(database,counter) -- database is an open file
> line = gets(database) -- line is a sequence
> index = find(',',line)
> last_name = line[1..index-1]
> counter = counter+80 -- line length, suppose
>end while
>Is this solution viable? Is there a better way if I can use the "easy" way
>you describe? Thanks.
No. You're a bit confused. Suppose you records are 100 bytes long, so to
read a record you do:
sequence record
integer i, fn, offset, counter
counter = 1
fn = open("database.dat","rb") -- Note that you must open the file as binary
while record != something do
seek(fn, 100 * (counter-1))
for i = 1 to 100 do -- Read 100 consecutive bytes from file
record = record & getc(fn)
end for
counter = counter+1
end while
This kind of searching is called secuential because you start from the first
record until you find what you where looking for. Once you've understand
this you can start experimenting with more sophisticated searching
algorithms (external indexes, binary search, binary trees, hash tables,
etc.).
Regards,
Daniel Berstein
daber at pair.com
|
Not Categorized, Please Help
|
|