Q(uick)BASIC Statement (File I/O): PUT

Quick View

PUT

A file I/O statement that writes from a variable or a random-access buffer to a file

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • GET [#]filenumber%[,[recordnumber&][,variable]]
  • PUT [#]filenumber%[,[recordnumber&][,variable]]
Description/Parameter(s)
filenumber% The number of an open file.
recordnumber& For random-access files, the number of the record to read or write. For binary-mode files, the byte position where reading or writing starts.
variable For GET, a variable used to receive input from the file. For PUT, a variable that contains output to write to the file. The variable is usually a variable of a user-defined data type.
Example
TYPE TestRecord Student AS STRING * 20 Score AS SINGLE END TYPE DIM MyClass AS TestRecord OPEN "FINAL.DAT" FOR RANDOM AS #1 LEN = LEN(MyClass) MyClass.Student = "MarySa" MyClass.Score = 99 PUT #1, 1, MyClass CLOSE #1 OPEN "FINAL.DAT" FOR RANDOM AS #1 LEN = LEN(MyClass) GET #1, 1, MyClass PRINT "STUDENT:", MyClass.Student PRINT "SCORE:", MyClass.Score CLOSE #1 KILL "FINAL.DAT"
Syntax
  • PUT [#]filenumber[,[recordnumber][,variable]]
  • PUT [#]filenumber[,{recordnumber|recordnumber,variable|,variable}]
Description/Parameter(s)
Argument Description
filenumber The number used in the OPEN statement to open the file.
recordnumber For random-mode files, the number of the record to be written. For binary-mode files, the byte position in the file where writing is done. The first record in a file is record 1. If you omit recordnumber, the next record or byte (the one after the last GET or PUT statement, or the one pointed to by the last SEEK) is written to. The largest possible record number is 2^31 -1 or 2,147,483,647.
variable The variable containing the output to be written to the file. The PUT statement writes as many bytes to the file as there are bytes in the variable.
If you use a variable, you do not need to use MKI$, MKL$, MKS$, or MKD$ to convert numeric fields before writing. You may not use a FIELD statement with the file if you use the variable argument.
For random-access files, you can use any variable as long as the length of the variable is less than or equal to the length of the record. Usually, a record variable defined to match the fields in a data record is used.
For binary-mode files, you can use any variable.
When you use a variable-length string variable, the statement writes as many bytes as there are characters in the string's value. For example, the following two statements write 15 bytes to file number 1:
  • VarString$=STRING$ (15, "X")
  • PUT #1,,VarString$
See the examples below for more information about using variables rather than FIELD statements for random-access files.
A record cannot contain more than 32,767 bytes.

You can omit the recordnumber, the variable, or both. If you omit only the recordnumber, you must still include the commas:

  • PUT #4,,FileBuffer

If you omit both arguments, you do not include the commas:

  • PUT #4

The GET and PUT statements allow fixed-length input and output for BASIC communications files. Be careful using GET and PUT for communications because PUT writes a fixed number of characters and may wait indefinitely if there is a communications failure.

Note: When using a file buffer defined by a FIELD statement, LSET, RSET, PRINT # , PRINT # USING, and WRITE # may be used to put characters in the random-file buffer before executing a PUT statement. In the case of WRITE # BASIC pads the buffer with spaces up to the carriage return. Any attempt to read or write past the end of the buffer causes an error message that reads "FIELD overflow."
Example

This example reads names and test scores from the console and stores them in a random-access file.

' Read a name and a test score from the console. ' Store each name and score as a record in a ' random-access file. ' Define record fields. TYPE TestRecord NameField AS STRING * 20 ScoreField AS SINGLE END TYPE ' Open the test data file. DIM FileBuffer AS TestRecord OPEN "TESTDAT.DAT" FOR RANDOM AS #1 LEN = LEN(FileBuffer) ' Read pairs of names and scores from the console. CLS ' Clear screen I = 0 DO I = I + 1 INPUT "Name ? ", FileBuffer.NameField INPUT "Score? ", FileBuffer.ScoreField INPUT "-->More (y/n)? ", Resp$ PUT #1, I, FileBuffer LOOP UNTIL UCASE$(MID$(Resp$, 1, 1)) = "N" PRINT I; " records written." CLOSE #1
Syntax
  • PUT [#]filenumber%[,[recordnumber&][,variable]]
Description/Parameter(s)
filenumber% The number used in the OPEN statement to open the file.
recordnumber& For random-access files, the number of the record to be written. For binary-mode files, the byte position where writing is done.
variable A variable that contains output to be written to the file.
  • If you omit the argument recordnumber&, the next record or byte (the one after the last GET or PUT statement, or the one pointed to by the last SEEK) is written to. The largest possible record number is 2^31 -1 or 2,147,483,647.
  • The first record or byte position in a file is 1.
  • A record cannot be longer than 32,767 bytes.
  • If you specify a variable, you do not need to use MKI$, MKL$, MKS$, MKD$, or MKC$ to convert numeric fields before writing. You can not use a FIELD statement with the file if you use the variable argument.
  • For random-access files, you can use any variable as long as the length of the variable is less than or equal to the length of the record. Usually, a record variable defined to match the fields in a DATA record is used.
  • For binary-mode files, you can use any variable. The PUT statement writes as many bytes to the file as there are bytes in the variable.
  • When you use a variable-length string variable, the statement writes as many bytes as there are characters in the string's value. For example, the following two statements write 15 bytes to file number 1:

  • VarString$ = STRING$(15, "X")
  • PUT #1, , VarString$

  • See the examples for more information about using variables rather than FIELD statements for random-access files.

Usage Notes

  • Do not use PUT on ISAM files.
  • You can omit the argument recordnumber&, variable, or both. If you omit the record number but include a variable, you must still include the commas:

  • PUT #4, , FileBuffer

  • If you omit both arguments, do not include the commas:

  • PUT #4

Important

  • GET and PUT statements allow fixed-length input and output for BASIC communications files. Be careful using GET and PUT for communications because PUT writes a fixed number of characters and may wait indefinitely if there is a communications failure.

Programming Tips

  • When using a file buffer defined by a FIELD statement, LSET, RSET, PRINT # , PRINT # USING, and WRITE # may be used to put characters in the random-file buffer before executing a PUT statement.
  • In the case of WRITE #, BASIC pads the buffer with spaces up to the carriage return. If you attempt to read or write past the end of the buffer, BASIC generates the error message, "FIELD overflow."
Example

This example use the PUT statement to store test scores in a random-access file. The TYPE statement is used to create a user-defined variable type.

'Define record fields. TYPE TestRecord NameField AS STRING * 20 ScoreField AS SINGLE END TYPE DIM FileBuffer AS TestRecord DIM I AS LONG 'Open the test data file. OPEN "TESTDAT.DAT" FOR RANDOM AS #1 LEN = LEN(FileBuffer) 'Read pairs of names and scores from the console. CLS 'Clear screen. I = 0 DO I = I + 1 INPUT "Name ? ", FileBuffer.NameField INPUT "Score? ", FileBuffer.ScoreField INPUT "-->More (y/n)? ", Resp$ PUT #1, I, FileBuffer LOOP UNTIL UCASE$(MID$(Resp$, 1, 1)) = "N" PRINT I; " records written." CLOSE #1 'Remove file from disk. KILL "TESTDAT.DAT"