Put writes one extra byte... 
Author Message
 Put writes one extra byte...

I'm trying to do some compression on custom binary files.  Before I
began the compression algorithm, I wanted to see the Get and Put
statements in action, so I just opened the original file and wrote it
to a temp copy.  I assumed that these two file would be identical.
However, on each attempt there is an extra byte (0x00) at the end of
the temp file.  I can find no explanation in any documentation as to
why this is.  Does anyone know what's causing this or how to fix it?

   Dim abBuf() As Byte

       nOriginalFile = FreeFile()
      Open strOriginalFile For Binary Access Read Lock Read Write As
#nOriginalFile
      nTempFile = FreeFile()
      Open strTempFile For Binary Access Write Lock Read Write As
#nTempFile

   ReDim abBuf(LOF(nTplFile))
   Get #nOriginalFile, 1, abBuf
   Put #nTempFile, 1, abBuf

Any feedback would be greatly appreciated.



Fri, 17 Aug 2001 03:00:00 GMT  
 Put writes one extra byte...
Regardless of how you set your Option Base, VB(5+) always creates an array
with one more element than
you declare. So, when your array of bytes is redimmed on the filesize, it
will be from zero to filesize, not 1 to filesize or zero to filesize -1.

Quote:

>   ReDim abBuf(LOF(nTplFile))

gives you a byte array one larger than the actual file size.

dim x(5) as byte creates
x(0), x(1), x(2), x(3), x(4), x(5)



Fri, 17 Aug 2001 03:00:00 GMT  
 Put writes one extra byte...

Hi John,

(snip one-too-many bytes)

<   ReDim abBuf(LOF(nTplFile))

The LBound of this Byte array is goning to be 0 so, if nTplFile
= nOriginalFile, use

    ReDim abBuf(LOF(nTplFile) - 1)

<   Get #nOriginalFile, 1, abBuf
<   Put #nTempFile, 1, abBuf

HTH
--
Paul Marshall



Fri, 17 Aug 2001 03:00:00 GMT  
 Put writes one extra byte...
Nope.  When Option Base 1 is in effect, ReDim abBuff(LOF(nTplFile)) will
give you an array exactly as large as the file.  It's just as though
you'd said ReDim abBuff(1 To LOF(nTplFile))... that's what Option Base
does.

In any case, it's always advisable to explicitly state both lower and
upper bounds.  You never know when your code will be reused in an
environment where Option Base is different than in the original project.

--
     Jim Mack
     MicroDexterity, Inc

     http://www.microdexterity.com



Quote:

>Regardless of how you set your Option Base, VB(5+) always creates an
array
>with one more element than
>you declare. So, when your array of bytes is redimmed on the filesize,
it
>will be from zero to filesize, not 1 to filesize or zero to
filesize -1.


>>   ReDim abBuf(LOF(nTplFile))

>gives you a byte array one larger than the actual file size.

>dim x(5) as byte creates
>x(0), x(1), x(2), x(3), x(4), x(5)



Fri, 17 Aug 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Put Byte Array Descriptor Length = 12 bytes?

2. Add an extra cell to only one row in MSHFLexGrid

3. Help : AppendChunk to put in BLOB data adds an extra byte !!!!!!

4. Help : AppendChunk to put in BLOB data adds an extra byte !!!!!!

5. Help : AppendChunk to put in BLOB data adds an extra byte !!!!!!

6. how to put exactly 1 byte to COM

7. writing more than one vbscript statement in one line

8. Need to convert a 16 byte array of bytes to 4 byte integer

9. Efficient method to convert 4 bytes in one Int32 signed integer

10. Read and Write Bytes?

11. Writing/copying bytes

12. Read/Write bytes from IO Port?

 

 
Powered by phpBB® Forum Software