Page 1 of 1

saving to a file with a 2 dimential array using records

Posted: Sun Dec 07, 2008 5:48 pm
by hugh
the put and get command put #1,record#, varible

i enter it put#1, array(x,y), array(x,y) and put#1,I,array(x,y)

and it doesn't except it

what is a correct way to enter it

Posted: Sun Dec 07, 2008 8:29 pm
by burger2227
GET and PUT use a certain syntax. They can both be used when a file is Opened FOR RANDOM. They can only use ONE variable and you should start from record one when writing the file initially or leave the recordplace blank if consecutive :

Get #filenumber, recordplace, recordvalues ' reads a Random file

PUT #filenumber, recordplace, recordvalues ' writes data to file

PUT #1, 1, value%
An example of writing ONE value from each record. What LEN did you assign the file when opening it? An Integer is 2 bytes.

OPEN filename$ FOR RANDOM AS #1 LEN = 2

If you are using just one Integer value per record, then you could hold
100 records in a 200 byte file. Use LOF(1) \ LEN(record) to find the number of records already entered. The SEEK function and statement can find and move to places in a file.

If you are using more than one value, use a TYPE Declaration to Get or PUT all of the values at once. You cannot use an Array inside the TYPE!

TYPE RecordType
First AS INTEGER '2 bytes
Second AS INTEGER '2 bytes

DIM SHARED Records AS RecordType 'shared passes to SUB programs

Required to use the one variable to hold more than one. Then you use DOT variables to hold each piece of information.

Records.First = Array(1, x)
Records.Second = Array(2, y)

PUT #1, 1, Records ' places both pieces of data in the record.


Posted: Sun Dec 07, 2008 8:39 pm
by Mentat
I had a little trouble with putting in the code. I'd kludge it by treating it as a linear array.

Like this:

Code: Select all

'The "size" of our 2d array
const arrayWidth = 12
const arrayHeight = 11

'our arrays
dim my2dArray(1 to arrayWidth, arrayHeight) as integer
dim my1dArray(1 to arrayWidth * arrayHeight) as integer

'Code for doing stuff with putting stuff in 2d array

'Now that we want to ship the bugger out
for i = 1 to arrayWidth
    for j = 1 to arrayHeight
        'Stuff the 1d array
        my1dArray(i + (j-1) * arrayWidth) = my2dArray(i, j)
    next j
next i

'To the file
open "myFile.dat" for binary as #1
for i = 1 to arrayWidth * arrayHeight
    put #1, i * 2, my1dArray(i)
next i

'Now that the party is over
close #1
*Note: I looked at the output, and it was fine. I changed the code a little bit though.

[edit]It looks like Burger has it better.

putting records into a 2d, random file

Posted: Wed Dec 10, 2008 4:49 pm
by hugh
this isn't the whole program the rest uses the 26 for the alphabet
to sort the names

TYPE people
name AS STRING * 30
job AS STRING * 30
age AS STRING * 3
10 CLS
DIM SHARED ray(26, 2) AS people
OPEN "c:\docume~1\publib2\desktop\" FOR RANDOM AS #1 LEN = LEN(ray(o%, p%))

FOR o% = 1 TO 6
DATA a,b,c,d,e,f,g,h,i,j,k,l
IF p% = 2 THEN GOTO 30
ray(o%, p%).name = a$
ray(o%, p%).job = "0"
ray(o%, p%).age = "0"
'PRINT ray(o%, p%).name;
PUT #1, o%, ray(o%, p%)

30 READ a$
ray(o%, p%).name = a$
ray(o%, p%).job = "0"
ray(o%, p%).age = "0"

PUT #1, p%, ray(o%, p%)
GET #1, o%, ray(o%, p%)
PRINT ray(o%, p%).name;
GET #1, p%, ray(o%, p%)
PRINT ray(o%, p%).name;

40 NEXT p%
KILL "c:\docume~1\publib2\desktop\"