11-28-2024, 02:18 PM
(11-28-2024, 02:02 PM)Petr Wrote: Nice work, Steve. It depends on the programmer's approach, I realized. While you are providing a way to do it, I was looking for reasons not to do it
It will work via binary access as you write here, but it won't work via Input and a file opened for reading, because some characters control the behavior of the input command in Open for Input mode.
The following program shows how binary text can confuse the Input command when reading a binary file.
Code: (Select All)
For a = 0 To 255
f$ = Chr$(a) + f$
Next a
ff = FreeFile
Open "binTest" For Binary As ff
Put ff, , f$
Close ff
ff = FreeFile
_ControlChr Off 'not for operations in file, just for printing...
Open "binTest" For Input As ff
Dim b As String
Do Until EOF(ff)
Input #ff, b$ 'or try LINE INPUT, returned string is not returned in full lenght
Sleep
Print b$, Len(b$)
Loop
Close ff
It's not supposed to. If you need to read a full binary range of characters (from 0 to 255 ASCII), you can't use INPUT. There are various old-school control codes embedded into INPUT, which are necessary for us to honor so that old QB45 programs can still compile and run the same as always with files written using those rules.
CHR$(26) is one of those terminal characters, if I remember correctly (it's CTRL-Z) and if basically counts much the same as an EOF marker. With it you used to be able to write a data file like so:
Red Bus, 1, 2, 3, 4, CHR$(26), Blue Bus, 1, 2, 3, 4, CHR$(26)....
Now, you could OPEN that file and read to EOF, and you'd only read to the CHR$(26) character. INPUT would basically return
Red Bus
1
2
3
4
IF you wanted the second file of data, you would SEEK #1, proper_position and then do the INPUT so you get
Blue Bus
1
2
3
4
(CHR$(26) as EOF marker)
Think of it as an old-school way of packing multiple text files together into one single file. You had your own individual data streams, all separated by that control character.
Reading a file as BINARY, you don't have those control characters. You'd have to account for them yourself, if you program made use of them or not...
But they're the reason why you're seeing breaks when using INPUT to read a BINARY file. INPUT was never made to read the whole 256 ASCII character range. It was made more for text input, with control characters embedded into the stream to help organize it into better and more manageable data.