Binary files in basic, how ?

Page 1/3
| 2 | 3

By WORP3

Paladin (804)

WORP3's picture

14-06-2010, 18:31

Maybe a stupid question and i almost know i did it myself a long time ago,
how can you read binary files inside msx basic ?

Normally you would use:

open "filename" for input as #1

var = input$(1,#1)

close #1

But this will go wrong if the binary file has characters inside it which will corrupt the file handler.

In qbasic you can use : open "filename" for binary as #1 but this doesn't work within msx basic Sad

Has anyone has an idea ?

Cheers,
Tjeerd.

Login or register to post comments

By Manuel

Ascended (15750)

Manuel's picture

14-06-2010, 20:51

How about this:

open "filename" for input as #1
input #1,X
close #1

X will contain the value (a byte, I guess), I think... But maybe there's more to it?

By WORP3

Paladin (804)

WORP3's picture

14-06-2010, 21:09

Nope already did try that, it doesn't work Sad

By WORP3

Paladin (804)

WORP3's picture

14-06-2010, 21:09

Nope already did try that, it doesn't work Sad

By flyguille

Prophet (3029)

flyguille's picture

14-06-2010, 21:31

... as input as #.... it opens for text format input..... I means

Text + CRCL
Text + CRCL

when you do a Input#1, X

BASIC will read the entire line, discart the CRCL, put it in a alphanumeric buffer, then do VAL($)

..............................................

to works accessing binaries files

it is

open "filename" for ramdon as#1 LEN=1 ???? i just don't remember acurately

then use

x$=string$( lenght_that_you_want_to_read, " ")
get#1, register_number, x$ (lee)

put#1, register_number, X$ (escribe)

if you writes over the end of the file, the file will be expanded.

surely is full of bugs, somebody correct it. Anyway it is on the MSX DISK BASIC manual

as LEN = 1 , register_number = Byte position inside the file starting counting from 1 (not zero).

By NYYRIKKI

Enlighted (5382)

NYYRIKKI's picture

14-06-2010, 21:36

Ugh... MSX-BASIC is very good doing pretty much anything... how ever reading / writing binary files is something that it is not good for... Usually at this point I end up filling my BASIC listing with USR-commands that handle the files, move to Nestor BASIC or do something else like that...

Anyway... if you really want to... then it goes something like this...

open "<filename.ext>" as #1 len=1
field #1,1 as f$
get #1,<bytenumber>
a$=f$
... now a$ has the byte that has been read... if you want to write it back, then it gets even more bizarre.... something like:

lset f$=a$: put #1,<bytenumber>

... totally different logic than in any other MSX-BASIC commands, I hate it...

By AuroraMSX

Paragon (1901)

AuroraMSX's picture

14-06-2010, 22:21

and what about a simple bload and peek combination?

By WORP3

Paladin (804)

WORP3's picture

14-06-2010, 22:35

That is amazing, thanks man, i really was waiting for that answer Big smile

open "<filename.ext>" as #1 len=1
field #1,1 as f$
get #1,<bytenumber>
a$=f$

It is working but it's really slow when processing a byte for byte Sad
Is it possible to define that field thing as a (for example) 64 bytes buffer instead of a F$ ?

But anyways thanks LOL!

By WORP3

Paladin (804)

WORP3's picture

14-06-2010, 22:39

Answer on that bload and peek idea:

Then i have to change my binary file which i don't like doing.

By NYYRIKKI

Enlighted (5382)

NYYRIKKI's picture

14-06-2010, 23:12

Is it possible to define that field thing as a (for example) 64 bytes buffer instead of a F$ ?

Actually that F$ is not reqular variable but a buffer or "record"... that is why I read the content to variable... I know... that is weird, not very logical etc.

If you want to handle this as 64 byte blocks, then you can do it like this:

open "<filename.ext>" as #1 len=64
field #1,64 as f$
get #1,<block number>

(block 1 = bytes 0-63, block 2 = 64-127 etc.)

Are you sure, you don't want to use NestorBASIC instead? Face

By NYYRIKKI

Enlighted (5382)

NYYRIKKI's picture

14-06-2010, 23:56

I'm not actually sure what you are trying to do, but I think that your program looks now something like this:

10 open "<filename.ext>" as #1 len=1
20 field #1,1 as f$
30 for i=100 to 130:get #1,i+1:n$=n$+f$:next i
40 close
50 print n$

.... and that is too slow... I have not tested if this is any faster (or even works), but you might want to try it...

10 defint a-z
20 open "<filename.ext>" as #1 len=64
30 field #1,64 as f$:ad=peek(varptr(f$)+1)+peek(varptr(f$)+2)*256
40 for i=100 to 130:gosub 70:n$=n$+chr$(a):next i
50 close
60 print n$:end
70 bl=(i\64)+1:if bl<>ob then get #1,bl:ob=bl
80 a=peek(ad+(iand63))
90 return

Page 1/3
| 2 | 3