PDA

View Full Version : File_Open (Random) Question



marcuslee
27-03-2009, 03:59
n = FILE_OPEN(FileName, Mode, [RecordSize])

Is it possible to divide the RecordSize in a Random File? I've done it in other basic languages. You can specify what is in the record and you can refer to just one part of it in reading or writing to the file.

Here's how it looks in Liberty Basic:



open "File.dat" for random as #dat len = 10
field #dat, 5 as FirstPart$
field #dat, 5 as SecondPart$


Then, later in the program, you can refer to just one part of the record:



FirstPart$ = "76103"
put #dat, RecordNum


In this case, nothing is written to SecondPart$ if I understand how it works.

Can this be done in thinBasic? Use Random files almost as mini-databases?


Mark

Michael Clease
28-03-2009, 10:59
Powerbasic does seem to support this type file access but Eros doesnt seem to of implemented the file handling in quite the same way.

but there is no reason you couldnt write your own handler, just use the seek keyword to find the next record.



Status = FILE_Seek(FileHandle, recordnumber*recordlength) ' Set the current read position to 1st byte of the record
buffer = FILE_Get (FileHandle, recordlength) ' Read recordlength bytes and add to buffer

marcuslee
28-03-2009, 14:42
Then, I suppose to finish the script, you could do something like this:



' Set the current read position to 1st byte of the record
Status = FILE_Seek(FileHandle, recordnumber*recordlength)
' Read recordlength bytes and add to buffer
buffer = FILE_Get (FileHandle, recordlength)
' Extract the first part of the record
firstpart = Mid$(buffer, 1, 5)
'Extract the second part of the record
secondpart = Mid$(buffer, 6, 5)


I've been looking around the net and have found the code structure below.



TYPE Person
LName AS STRING * 12
FName AS STRING * 8
Age AS INTEGER
END TYPE

DIM P AS Person

FileHandle = FILE_OPEN( FileName, "Random", LEN( P ))


I'm guessing that it won't work with thinBasic. If so, how do you determine RecordLength? By knowing the length of each field in your file? For example, if you know that one of your fields is a five digit number and that your second field is a string that you allot 10 characters in length, do you set the RecordLength at 15? If I'm not making much sense, I'm sorry. I'm having a hard time trying to explain this.

Arg!! :violent:


Mark

Michael Clease
28-03-2009, 15:48
I removed this post as it didnt work.

Petr Schreiber
28-03-2009, 15:59
Mike was faster,

here is mine version :):


uses "File", "Console"

type tRecord
LName AS STRING * 12
FName AS STRING * 8
Age AS BYTE
end type

dim item as tRecord

file_kill("test.txt")

dim f as dword = file_open("test.txt", "binary")

item.LName = "Vader"
item.FName = "Darth"
item.Age = 40
printl "Vader in the file"
file_put_tRecord(f, item)

item.LName = "Yoda"
item.FName = "Mister"
item.Age = 45
printl "Yoda in the file"
file_put_tRecord(f, item)


print "Seeking first: "
file_seek_tRecord(f, 1)
item = file_get_tRecord(f)
printl item.LName, item.FName, item.Age

print "Seeking second: "
file_seek_tRecord(f, 2)
item = file_get_tRecord(f)
printl item.LName, item.FName, item.Age

file_close(f)

waitkey

function file_seek_tRecord(f as long, nth as long)
file_seek(f, (nth-1)*sizeof(tRecord)+1)
end function

function file_put_tRecord(f as long, item as tRecord)
file_put(f, PEEK$(varptr(item), sizeof(item)))
end function

function file_get_tRecord(f as long) as string
function = file_get(f, sizeof(item))
end function



Petr

Lionheart008
30-03-2009, 11:15
hi petr:) , hi all...

@petr: please can you test your script if it's running? I cannot do that... have got error messages... or I have done something wrong... ???

stop: script is ok... have copied something wrong, thank you ;) I was too silly... :violent:

best regards, Lionheart

Lionheart008
31-03-2009, 13:26
hi marcuslee, petr, all:)

have tested the interesting script and changed it with two new heroes to catch the knowledge :)...

only the result as *.txt file isn't so clear as sentence for me... I am wrong with something in the code, isn't it??? perhaps somebody can check it... would be nice...

best regards, Lionheart
("mittagspause")

Petr Schreiber
31-03-2009, 13:41
Frank,

the output file is binary - that is why strings look ok, but numbers are "odd".
Member Age is of type BYTE = 8 bits = 1 ASCII character.
Member Hero is of type LONG = 32 bits = 4 ASCII characters.


Petr

ErosOlmi
05-04-2009, 12:07
I've added 2 new functions that will allow to work directly with random files:



FILE_PUTR(fChannel, RecNumber, UDTVAriable) '---Used to store structures
FILE_GETR(fChannel, RecNumber, UDTVariable) '---Used to read back structures


Features will be present in next release.

Ciao
Eros

efly2020
05-04-2009, 21:02
Thank YOU.
I needed those.

Ralph

ErosOlmi
16-04-2009, 05:38
Those 2 new functions are now present in thinBasic beta 1.7.8.0 that is available for testing at http://community.thinbasic.com/index.php?topic=2588.0