Results 1 to 3 of 3

Thread: Read files into Byte array - Common mistake

  1. #1

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    Read files into Byte array - Common mistake

    Hello everyone!

    I did a little search for file reading methods and the one I've found for reading Byte arrays looks like this (short form):

    VB Code:
    1. ReDim Preserve Temp(LOF(FileNum))
    2. Get FileNum, , Temp

    I really wonder, why people didn't yet see this is wrong... the code above reads 1 more byte than necessary because LOF returns the number of bytes where arrays go from 0 to x and not from 1 to x ! The correct code thus would be:

    VB Code:
    1. ReDim Preserve Temp(LOF(FileNum) [b]- 1[/b])
    2. Get FileNum, , Temp

    (border notice: Preserve is faster - always use it when you're going to overwrite the data anways)

    Beware yourself making this error! And you should always be careful when copying other people's code - better test it out before using it!

    Fox

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    btw, you have a slight *mishap* there. If the file does not exists or has a lengh of zero, it will fail, arrays can not be size -1 (unless you have a split function )

  3. #3

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    I was just showing the basics The checks if there's anything to do at all should of course happen before doing anthing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width