GET with BINARY File Fills String with NULL Bytes at EOF (39380)






This article was previously published under Q39380

SUMMARY

Using the GET #n statement with a file that has been opened for BINARY will fill the last bytes of an existing input string (which is the third parameter of GET) with NULLs. This only occurs if the length of the existing string exceeds the number of bytes between the file pointer (before the GET) and the end of file. This behavior applies to both fixed-length and variable-length string variables.

This information applies to Microsoft QuickBasic Versions 4.00, 4.00b, and 4.50, and to Microsoft Basic Compiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2. (Earlier versions do not have a third parameter for the GET#n statement, and are not affected.)

MORE INFORMATION

The example program below demonstrates how this occurs. If TestString$ = "123456789012345" and TEST.DAT is a text file OPENed in BINARY containing only the string "TEST", the following GET statement will alter TestString$ such that it contains the string "TEST" and 11 NULL bytes.

The following is a code example:
CLS
OPEN "TEST.DAT" FOR BINARY AS #1
TestString$ = "123456789012345"
PRINT "ASCII dump of string BEFORE GET from small file"
FOR i = 1 TO 15: PRINT ASC(MID$(TestString$, i, 1)); : NEXT i
GET #1, 1, TestString$   ' This GET requires QuickBasic 4.x
PRINT "ASCII dump of string AFTER GET from small file"
FOR i = 1 TO 15: PRINT ASC(MID$(TestString$, i, 1)); : NEXT i
END
				
The output is as follows:

ASCII dump of string BEFORE GET from small file
49 50 51 52 53 54 55 56 57 48 49 50 51 52 53
ASCII dump of string AFTER GET from small file
84 69 83 84 0 0 0 0 0 0 0 0 0 0 0


Modification Type: Minor Last Reviewed: 1/8/2003
Keywords: KB39380