October 30, 2025, 05:29:07 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Random Access Files

Started by Andy, October 13, 2011, 11:19:44 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

Sorry for posting another question but I can't find the answer to what I would have thought of as a simple problem?

I need to create a random access file containing record numbers and strings.

e.g.       record no = 7
            string      = "The Winner"

I have taken the help file example as follows:

TYPE HighScoreTopTen
     DEF Score as INT
     DEF Time as INT
     DEF pname[20] as ISTRING
ENDTYPE

DEF TopTen as HighScoreTopTen

DEF myfile as BFILE

IF(OPENFILE(myfile,"C:\\HIGHSCORES.DAT","W") = 0)

TopTen.Score = 100
TopTen.Time = 25
TopTen.pname = "The Winner"

PUT myfile,7,TopTen

TopTen.Score = 100
TopTen.Time = 25
TopTen.pname = "Second place"

PUT myfile,8,TopTen

CLOSEFILE myfile
ENDIF

TopTen.pname = "The Winner"


OPENCONSOLE

IF OPENFILE(myfile,"C:\\HIGHSCORES.DAT","R") = 0
GET myfile,7,TopTen
CLOSEFILE myfile
ENDIF

PRINT TopTen.pname
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END

The question is this:

How can i search for the record "The Winner" when I do not know the record number? I do remember there was a way of defining fields as "Key" or "Index" fields that you could search on instead of the record number.  Can this be done?

I know I can use a database but it just seems a bit over the top for what I need.

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

Since this example was for top ten it means it is a short file.
The code below will handle it just fine.

Once you reach a certain file size it becomes more advantageous to use a dbase file with its indexing capabilities.

TYPE HighScoreTopTen
     DEF Score as INT
     DEF Time as INT
     DEF pname[20] as ISTRING
ENDTYPE

DEF TopTen as HighScoreTopTen

DEF myfile as BFILE

IF(OPENFILE(myfile,getstartpath+"HIGHSCORES.DAT","W") = 0)

TopTen.Score = 100
TopTen.Time = 25
TopTen.pname = "The Winner"

PUT myfile,7,TopTen

TopTen.Score = 100
TopTen.Time = 25
TopTen.pname = "Second place"

PUT myfile,8,TopTen

CLOSEFILE myfile
ENDIF

Searchname = "The Winner"


OPENCONSOLE

IF OPENFILE(myfile,getstartpath+"HIGHSCORES.DAT","R") = 0
int num_records = len(myfile)/sizeof(TopTen)
for x=1 to num_records
GET myfile,x,TopTen
if lcase$(searchname)=lcase$(TopTen.pname)
breakfor
endif
next x
CLOSEFILE myfile
ENDIF

PRINT TopTen.pname
PRINT TopTen.score
PRINT TopTen.time

DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END


LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Andy

Thanks Larry,

That works fine for me at the moment, if the file size / search time becomes to big / slow then I will look at using a database instead.

Once again thanks for your help.

Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.