Dear all

I'm trying to write a VB code to write a binary file. I already did the job but the problem is that an extra 00 byte is always added at the end of the file. I believe the problem is that after I finish writing, the file pointer moves to the next byte... but I'm not sure why this causes 00 to be written

Is there any way not to produce the extra 00 at the end of the file ???

I'm using VB6

Thanks so much.

Dani AI

Generated

Good catch by and good call from to ask for the code. In VB6 a binary write writes the raw memory of whatever you pass to Put, so the type and length of that variable decide how many bytes appear in the file. That means an extra 00 at the end most often comes from the value you wrote being larger or padded (fixed-length strings, UDT padding, Variants, numeric types bigger than intended), not from the file pointer mysteriously inserting a byte.

Quick checklist to diagnose and fix:

  • Use LenB(variable) to see how many bytes VB will write for that value.
  • Inspect the variable type with TypeName/VarType so you know its in-memory size.
  • After a write, check LOF(filenumber) or open the file in a hex editor to locate any trailing nulls.
  • When appending, use Binary mode with Seek/LOF + 1 rather than sequential append to avoid unexpected padding.
  • For exact control, build and write a Byte array or use the optional count argument of Put to limit how many bytes are written.

Example patterns (VB6):

Dim b() As Byte
ReDim b(0 To 0)
b(0) = &HFF

Open "out.bin" For Binary Access Write As #1
Put #1, , b
Close #1

If sending strings to an external binary format, convert to a byte array first (for example with StrConv(..., vbFromUnicode)) and write that. Be careful with UDTs and Variants — they can carry padding or metadata that adds bytes.

Recommended Answers

All 2 Replies

Post your code

Thx guys I found the problem.... :)

I defined the variable to be written to the file as Integer. while it should have been defined as byte, as Integer type is restored in two types !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.