Link to home
Start Free TrialLog in
Avatar of joekeri
joekeriFlag for United States of America

asked on

How to add record to a RANDOM file

I am tryign to add (PUT) a record to a random file. The problem i have is that the latest record that i add overwrites the previous record i added.


Somehow I think I need to now the record count of the file and incremeeent a global variable each time i PUT to the file.

Any ideas from anyone?

Avatar of Erick37
Erick37
Flag of United States of America image

Omit the location parameter, and Put will add the record to the end of the file...

Put #1,,userdata
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joekeri

ASKER

hmmmm,, i tried the codign u suggested and it stil overwrite steh last record added.

Here is my code.. Maybe its soemthign i am doing wrong.

Dim outnum As Integer, outrec As datain
Open "c:\librarian\clients_random.dat" For Random As #1 Len = Len(outrec)


outrec.din_name = txt_lastname.Text & ", " _
                  & txt_firstname.Text

Put #1, , outrec
txt_add_msg.Text = "** ADDED ** " & outrec.din_name
Close #1
Erick37,

Omitting the recordnumber doesn't put the record at the end of the file.  It writes the data wherever the file pointer currently is.  If the file was just created, then the filepointer will happen to be at the end of the file and subsequent Puts will append records.   It is possible to move the file pointer however though the seek methord or by specifying a record number with a Get/Put statement.

~IM
Make sure that your type datain has fixed length strings.

e.g.

din_name As String * 40
So true Idle Mind, and as always your explanation is thorough.
Avatar of joekeri

ASKER

I figured it out.  The first this I do is to figure out how many records in the file and store the value in RECORDNUMBER.

Then i save MAXSIZE into a global variable called NEXT_RECORD_COUNTER.

Then I process the file as normal and use NEXT_RECORD_COUNTER as part of the PUT statement.

I tested this code and it works successfully.

Private Sub btn_add_Click()
Dim outnum As Integer, outrec As datain
Open "c:\librarian\clients_random.dat" For Random As #1 Len = Len(outrec)
maxsize = LOF(1) \ Len(outrec) 'get number of records in file
For recordnumber = maxsize To 1 Step -1
    Seek #1, recordnumber      ' set position
    Get #1, , outrec           ' read record
Next recordnumber
Close #1
txt_record_count.Text = maxsize
next_record_counter = maxsize

Open "c:\librarian\clients_random.dat" For Random As #1 Len = Len(outrec)
outrec.din_name = txt_lastname.Text & ", " _
                  & txt_firstname.Text
outrec.din_clientid = txt_client.Text
outrec.din_socsec = txt_socsec1.Text & "-" & txt_socsec2.Text & "-" & txt_socsec3.Text
outrec.din_date = Date$
outrec.din_whohasit = "** BACK IN FILE **"
next_record_counter = next_record_counter + 1
Put #1, next_record_counter, outrec
txt_add_msg.Text = "** ADDED ** " & outrec.din_name
Close #1

End Sub
OK but then let's say a few months later you need to add a data item to the record, now what?

I'm guessing you have to open the file, iterate thru all the records, converting to new format which adds new variable to the UDT and then write it out to a new file???

Advice in this area would be much appreciated.

Also, when setting the string length, does * 5 mean 5 characters?  Is there any significance to the number of characters as it relates to byte storage boundaries?  i.e. does * 64 correspond to one byte or ???  Just trying to align the lengths of the strings to byte boundaries if it makes sense to do so...
>> OK but then let's say a few months later you need to add a data item to the record, now what?

>> I'm guessing you have to open the file, iterate thru all the records, converting to new format which adds new variable to the UDT and then write it out to a new file???

Yes. You are correct.

>> Also, when setting the string length, does * 5 mean 5 characters?

Yes. You are correct.