Adding bytes to start of file

Just starting out? Need help? Post your questions and find answers here.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

Adding bytes to start of file

Post by matalog »

I'm working with an old file format, .tap. It requires the header to have information on the length of the contents of the data following it. Should I be creating the file content, then storing that in a file, then using the contents of the file to read into a new file as I generate the header? Is there a way to INSERT bytes into a file that will move the other contents up by the amount of bytes inserted?

I will also have to get a binary XOR of all of the bytes in the file for the checksum, is there a quick way to do that, or should I be thinking about some thing like:

Code: Select all

x.a=Byte0 ! Byte1
ctr=2
Repeat
x= x ! Byte(ctr)
ctr=ctr+1
Until ctr= Length of Bytes
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Adding bytes to start of file

Post by wilbert »

If you need to store the length as a number but don't know it in advance, you can store it as 0 initially and when you know the right value use FileSeek to the position where the length value is stored and overwrite the 0 with the correct value.
matalog wrote: Tue Jun 18, 2024 9:43 am I will also have to get a binary XOR of all of the bytes in the file for the checksum, is there a quick way to do that
It depends on what you call quick.
From what I read online ( https://sinclair.wiki.zxnet.co.uk/wiki/TAP_format ) the format is from the 80s.
I suppose the files are so short that any method will be fast enough.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Adding bytes to start of file

Post by jacdelad »

From what I read from wilberts link, the header is always 19 bytes long with each information always in the same place. I would create a structure for it, write the header and the data. Either fill the header with the real information beforehand or afterwards and write the header again.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

Re: Adding bytes to start of file

Post by matalog »

You're right guys, of course. I should be using a placeholder.

I don't program enough unfortunately. Thanks for the help :-).
Post Reply