home *** CD-ROM | disk | FTP | other *** search
- Storing Numbers In Random Files
-
- Peter Rice
- Athens IBM PC User Group
-
- No matter how expert you may be, it's
- always challenging to find your way
- around a new computer system. (New
- computer users, take heart: it is
- just as frustrating for experts as it
- is for you.) A case in point is the
- use of random files in Microsoft
- BASIC -- in particular, how to store
- and retrieve numbers from random
- files. I've had trouble with this
- myself, and others have told me of
- similar experiences.
-
- Your source of information on this
- matter is the BASIC Manual, pages
- 4-63, 4-70 and B-9 to B-11. The
- process could be better explained
- with more good examples given. So
- here is an explanation and an
- example.
-
- There are three kinds of numbers in
- BASIC: integers (stored in two
- bytes), single precision reals
- (stored in four bytes); and double
- precision reals (stored in eight
- bytes). BASIC knows which is which
- because of the way a variable is
- stored: one byte stores the type, the
- next three to x bytes store the
- variable name and the remaining bytes
- store the number.
-
- All data stored in a random file is
- handled as string data. The FIELD
- statement, which defines a record,
- identifies the data with string
- names. The functions which move data
- in and out of this record are string
- functions. Your numbers must
- therefore be changed to strings to
- get them in, and must be converted
- back when getting them out.
-
- One way to do this is to convert a
- number digit by digit into string
- digits: 0 becomes 48 (="0"); 1
- becomes 49 (="1"); etc. This is no
- problem -- in fact, CHR$ will do it
- nicely and VAL will convert back --
- but this takes up more space on the
- disk than is necessary. MKS$ will
- convert your single precision number
- to a string by changing the first
- byte in the storage location of the
- number, making BASIC think that the
- four bytes which hold the number are
- a string. Try PRINT MKS$(44.7) to see
- what I mean. Then the number is
- stored in four bytes rather than the
- eight bytes that might be needed to
- store the eight digits of the number
- -- A great savings in storage space.
-
- CVS will take a four byte string and
- convert it back. Of course, if the
- string wasn't created by MKS$ in the
- first place, garbage will result! To
- see this at work, enter the program
- below and run it.
-
- 100 OPEN "TRIAL.LST" AS #1 LEN=20
- 110 FIELD #1,2 AS INTEGER$,4 AS
- REALVAR$,8 AS DOUBLE$
- 120 'Get three numbers and store
- them in a record
- 130 PRINT"Enter an integer, a real
- number, a double precission
- number"
- 140 INPUT A%,B,C#
- 150 LSET INTEGER$=MKI$(A%)
- 160 LSET REALVAR$=MKS$(B)
- 170 LSET DOUBLE$=MKD$(C#)
- 180 PUT 31,#1
- 190 'Now read the record and convert
- back to numbers
- 200 GET #1,1
- 210 X%=CVI (INTEGER$)
- 220 Y=CVS (REALVAR$)
- 230 Z#=CVD (DOUBLE$)
- 240 PRINT X% "=" A%,Y "=" B,Z# "=" C#
- 250 END
-
- You will notice that the INPUT
- statement expects three numbers
- separated by commas before you hit
- the return key. Also, don't give it a
- number that it can't take, such as
- 35550 for an integer. (The maximum
- integer allowed is 32767.) The output
- should be three equalities, with the
- numbers on each side identical. Now
- try changing the length of the
- strings by changing the numbers in
- the FIELD statement. If a number is
- too small, an "illegal function call"
- results. If the string is too big, it
- works fine because only the leftmost
- portion of the string is used.
-
- Of course, if you should change an
- integer via MKI$ and store the result
- in a four byte string variable, then
- read it back with CVS, you will get
- garbage. It is like trying to read
- French with a German dictionary.
-
- Also, be careful about reading random
- file records that you have not
- written. A random file works on the
- principle that the record that you
- want is just so far into the file. If
- you have not written that far into
- the file, BASIC has no way of knowing
- that. (Sequential files have
- end-of-file marks, but random files
- do not.) This means that you can read
- things that were put there by other
- files that have since been erased. Of
- course, trying to use CVI on such
- data will get nonsense.
-
- The secret to getting these clever
- functions to work is to use them
- exactly right on strings of the right
- size.