Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/35654

From BetaArchive Wiki
Knowledge Base


"Bad Record Length" if PUT#1,,x$ Where x$ Length = Buffer Size

Article ID: 35654

Article Last Modified on 11/21/2006

This article was previously published under Q35654

SUMMARY

The following information about random file I/O with a variable-length string as the third parameter of a GET or PUT statement was taken from the UPDATE.DOC file for QuickBasic version 4.0b (see also the UPDATE.DOC file for Basic Compiler 6.0b):

PUT statement encodes the length of the string and stores it as the first 2 bytes of the string. The GET statement uses this encoded value to determine how many characters to read.


This behavior of PUT and GET applies to Microsoft Basic Compiler version 6.0b for MS-DOS and MS OS/2, to Microsoft QuickBasic versions 4.0b and 4.5 for MS-DOS, and to Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS and MS OS/2. This behavior only applies to variable-length strings, not to fixed-length strings.

This behavior of PUT and GET is different than in QuickBasic version 4.0 and Basic Compiler version 6.0, where the string length is not recorded by PUT or used by GET.

Note that the third argument of the PUT and GET statements was introduced in QuickBasic version 4.0, and in Basic Compiler version 6.0, and is not found in earlier versions.

MORE INFORMATION

You will get a "Bad Record Length" error at run time in a QuickBasic version 4.0b or 4.5 program that uses a variable-length string with a length equal to the record length of the OPENed random-file buffer as the third parameter of the PUT statement. Because the two-byte string length is written to the file in addition to the string itself, the record length specified in the "LEN=" clause in the OPEN statement must be at least 2 bytes longer than the variable-length string used as the third argument in a PUT statement.

Using a third parameter on the PUT or GET statement is not supported in versions of QuickBasic earlier than version 4.0.

When you PUT with a third parameter in a QuickBasic version 4.0 program, the length of the string variable is not written; thus you can PUT a string that is equal in length to the random-file buffer size without error. However, to ensure compatibility with later versions, you should add 2 bytes to the OPENed random-file buffer size.

The following example works in QuickBasic version 4.0 or in Basic Compiler version 6.0, but gives a "Bad Record Length" error at run time if compiled in QuickBasic version 4.0b or 4.5, or in Microsoft Basic Compiler version 6.0b, 7.0, or 7.1:

OPEN "junk.dat" FOR RANDOM AS #1 LEN = 15
FOR k = 1 TO 5   'The following string is 15 characters long:
   a$ = "123456789012345"
   PUT #1, k, a$
NEXT k
                

The following example, which adds 2 to the record-length value in the LEN= clause, works correctly in QuickBasic versions 4.0, 4.0b, and 4.5 and in Microsoft Basic Compiler versions 6.0, 6.0b, 7.0, and 7.1:

OPEN "junk.dat" FOR RANDOM AS #1 LEN = 17
FOR k = 1 TO 5   'The following string is 15 characters long:
   a$ = "123456789012345"
   PUT #1, k, a$
NEXT k
                

The following technique should be used to ensure compatibility with releases later than QuickBasic version 4.0 or later than Basic Compiler 6.0.

If you use a variable-length string as the third argument of a PUT statement to write to a random access file in QuickBasic version 4.0, then reading that record in QuickBasic version 4.0b or 4.5 may give you a "Bad Record Length for GET" error, or string input of the wrong length. This occurs because GET in QuickBasic versions 4.0b and 4.5 interprets the first 2 bytes after the variable-length string as the length, but version 4.0 did not put the expected two bytes there. To work around this compatibility issue, you can modify your version 4.0b or 4.5 program to GET into a fixed-length string of the correct length.


Additional query words: QuickBas BasicCom

Keywords: KB35654