saving to a file with a 2 dimential array using records

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
hugh
Newbie
Posts: 4
Joined: Sun Dec 07, 2008 5:37 pm

saving to a file with a 2 dimential array using records

Post 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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
END TYPE

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.

Ted
Last edited by burger2227 on Sun Dec 07, 2008 11:07 pm, edited 3 times in total.
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post 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
end
*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.
For any grievances posted above, I blame whoever is in charge . . .
hugh
Newbie
Posts: 4
Joined: Sun Dec 07, 2008 5:37 pm

putting records into a 2d, random file

Post 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
END TYPE
10 CLS
DIM SHARED ray(26, 2) AS people
OPEN "c:\docume~1\publib2\desktop\store.data" 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
FOR p% = 1 TO 2 '...PUTTING 0 IN A TWO DIMENTIONAL ARRAY
IF p% = 2 THEN GOTO 30
READ a$
'PRINT p%
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%)
GOTO 40

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%
SLEEP 1
NEXT o%
CLOSE #1
KILL "c:\docume~1\publib2\desktop\store.data"
END
Post Reply