robhagemans / pcbasic

PC-BASIC - A free, cross-platform emulator for the GW-BASIC family of interpreters
http://www.pc-basic.org
Other
393 stars 48 forks source link

Record length is ignored when writing RANDOM files #187

Closed Marrin closed 2 years ago

Marrin commented 2 years ago

PC-BASIC Version: 2.0.4

When opening a file for RANDOM access, the given record length is ignored and the default value of 128 is used.

Test case (assumes there is no file of that name already):

10 OPEN "test.txt" FOR RANDOM AS #1 LEN=1:PUT #1:CLOSE 1

The file is now 128 bytes in size, instead of the expected 1 byte:

$ ls -l TEST.TXT
-rw-rw-r-- 1 bj bj 128 Jun 26 13:04 TEST.TXT

How I stumbled upon this? Someone at Stack Overflow asked how to write (text) files without the Ctrl+Z character at the end:

10 FILENAME$="test.txt"
20 REM Delete any old content.
30 OPEN FILENAME$ FOR OUTPUT AS #1:CLOSE 1:KILL FILENAME$
40 REM Open file in random mode with record length of 1 and assign a field to C$.
50 OPEN FILENAME$ FOR RANDOM AS #1 LEN=1:FIELD #1,1 AS C$
60 TEXT$="Hello, World!"+CHR$(13)
70 REM Now PUT each character/byte into the file one by one.
80 FOR I%=1 TO LEN(TEXT$):LSET C$=MID$(TEXT$,I%,1):PUT #1:NEXT
90 CLOSE 1
robhagemans commented 2 years ago

Thanks for reporting! I can reproduce the bug and have checked vs. GW-BASIC (on DosBox). It's a regression as 1.2 did the right thing.

robhagemans commented 2 years ago

Fixed by commit 764f6feb