Why does BASIC append 0x1a to the end of a file?

I'm attempting to transfer files to an old PCjr via null modem serial cable. I cobbled up a quick BASIC script to read data from a serial port and write it to a file, but I've discovered every file I save has an extra byte, 0x1a, at the end.

It seems to be caused by the CLOSE statement, as even OPENing and CLOSEing a file without writing anything results in a one-byte file.

I don't want a SUB character at the end of my files, especially binary ones! Is there an alternate way of writing files that prevents this behavior?

(Using the excellent "PC-BASIC" in a Lubuntu VM for demonstration, but this seems to be a "thing" in all variants of BASIC)

josh2112@jf334-lubuntu-vm:~$ pcbasic -b
PC-BASIC 2.0.0                                                                  
(C) Copyright 2013--2018 Rob Hagemans.                                          
60300 Bytes free                                                                
OPEN "O",#1,"TEST.TXT":PRINT#1,"hello world";:CLOSE:SYSTEM                      
josh2112@jf334-lubuntu-vm:~$ xxd TEST.TXT 
00000000: 6865 6c6c 6f20 776f 726c 641a            hello world.


  • Maybe this can help:

    Batch script to merge files without Hex char 1A at the end

    The post discusses how DOS handles copying data with the EOF indicator and how the flags work. Could you batch the copying the files with the switches indicated?

    EDIT by question author: Yes, this is it. The specific command needed is copy src.bin /a dest.bin /b. This strips off the EOF byte from the end of the file. This is even supported way back in DOS 2.10! Annoying that I have to run this extra step after using my BASIC program to save the file, but it gets the job done.