Qbasicnews.com
April 22, 2018, 07:38:15 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: freefile to make appendable a random file?  (Read 5191 times)
guyisland
New Member

Posts: 2


« on: July 09, 2009, 06:31:20 PM »

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.
Logged
MystikShadows
Ancient Guru
****
Posts: 542



WWW
« Reply #1 on: July 09, 2009, 11:36:16 PM »

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. .
Logged

hen they say it can't be done, THAT's when they call me ;-).




need hosting:  http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
Clippy
Member
*
Posts: 84



« Reply #2 on: July 09, 2009, 11:56:49 PM »

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
Logged

guyisland
New Member

Posts: 2


« Reply #3 on: July 10, 2009, 11:26:39 AM »

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.
Logged
NilmTyNc73
New Member

Posts: 6


WWW
« Reply #4 on: November 16, 2009, 03:28:07 AM »

Oh gosh I dont even know how to save on to a CD.

Should I firstly delete all the stuff I dont want?
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!