Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
freefile to make appendable a random file?
#1
hy, i am trying to make a small database to practice, but i like to save a record in a file. for adding more file in the next time to open. my situation is that i used the free file to start in the next free record when add more record , but the new record delete the old record, not entire the file.

Code:
CLS
TYPE namestype
     nm AS STRING * 10
     ln AS STRING * 10
     age AS INTEGER
END TYPE
DIM names AS namestype
DIM fh AS INTEGER
fh = FREEFILE
recordlen# = LEN(names)
OPEN "stubase.dat" FOR RANDOM AS #fh LEN = recordlen#
5
INPUT "name: ", names.nm
INPUT "last name: ", names.ln
INPUT "age:", names.age
PUT #fh, , names
INPUT "any more data?", more$
       IF more$ = "y" GOTO 5 ELSE
CLOSE #fh
END

was is the best approach to make a file appendable and situable for a database, to editable the file and also, add more record.
thanks in advanced.
Reply
#2
Hi guyisland,

the main reason is because when you just open the file you have no way, in the code you have there, to know where to append the record.

Here's a modified version of your code that will always append it where it should, at the end of the file.

Code:
CLS
TYPE namestype
     nm AS STRING * 10
     ln AS STRING * 10
     age AS INTEGER
END TYPE
DIM names AS namestype
DIM fh AS INTEGER
DIM recordcount AS INTEGER ' To get number of records in the file.

fh = FREEFILE
recordlen# = LEN(names)
OPEN "stubase.dat" FOR RANDOM AS #fh LEN = recordlen#

5

' here is were I evaluate the number of records.
RecordCount = LOF(fh) / Recordlen#
PRINT "There are currently " + STR$(RecordCount) + " records in the database."

INPUT "name: ", names.nm
INPUT "last name: ", names.ln
INPUT "age:", names.age
PUT #fh, RecordCount + 1 , names
INPUT "any more data?", more$
       IF more$ = "y" GOTO 5 ELSE
CLOSE #fh
END

LOF gives you the lenght of the file in bytes.  if you divide that length by the size of a record (recordlen# in this case) it gives you the number of physical record in the database.  Run it once, and you'll see that every time you run it, you have an extra record added in the database instead of overwriting the record like your code was doing. .
hen they say it can't be done, THAT's when they call me ;-).

[Image: kaffee.gif]
[Image: mystikshadows.png]

need hosting: http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
Reply
#3
You need a record placeholder in the PUT otherwise it will write over beginning records. PUT assumes it's own consecutive record places if you dont. That is why you lost data.

PUT #fh, 6, names  'writes to the 6th record.  ALWAYS use a record number!
You can use a FOR loop or counter variable.

So how do you find out how many records are already in the file?

Code:
recordlen% = LEN(names) 'recordlen is always an Integer!

NumRecords% = LOF(fh) \ recordlen  'you may want to print the value.

To add records use PUT placeholders above the number of old records.

Ted
Get my QB demonstrator here: http://dl.dropbox.com/u/8440706/Q-Basics.zip
Reply
#4
hy, thanks a lot. now when i put information and new information in the database, the file reflect all update.
the essence code to make a random file appendable is:
Code:
DIM ff AS INTEGER
DIM recordcount AS INTEGER ' To get number of records in the file.

ff= FREEFILE
...
(assign "ff" in the open file)
RecordCount = LOF(ff) / Recordlen#
...
PUT #ff, RecordCount + 1 , ...
---------------------------------------------------------------
Thanks a lot!!!.
now my next code is to make a editable record.
my other project is the view table.  that display all record in the above file.

is at least 3 step
make (also add)
see
and edit.
Reply
#5
Oh gosh I dont even know how to save on to a CD.

Should I firstly delete all the stuff I dont want?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)