Results 1 to 11 of 11

Thread: overflow error

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    overflow error

    Help! I'm getting an overflow error on the red line below and I can't figure out why. FileLen returns 86,000+, so I don't think that's the problem... I don't get it.

    VB Code:
    1. Private Function ReadFileAsBinary(strSrcFilename As String, lngFileSize As Long, bytBuffer() As Byte) As Boolean
    2.  
    3.     Dim intFileHandle As Integer
    4.     Dim intSeekPos As Integer
    5.    
    6.     'On Error GoTo ErrHandler
    7.  
    8.     lngFileSize = FileLen(strSrcFilename)
    9.    
    10.     intFileHandle = FreeFile
    11.  
    12.     Open strSrcFilename For Binary As intFileHandle
    13.  
    14. [COLOR=RED]For intSeekPos = 1 To lngFileSize[/COLOR]
    15.         Get #intFileHandle, intSeekPos, bytBuffer(intSeekPos - 1)
    16.     Next intSeekPos
    17.    
    18.     Close intFileHandle
    19.  
    20.     ReadFileAsBinary = True
    21.  
    22.     Exit Function
    23.  
    24. ErrHandler:
    25.  
    26.     MsgBox "Error reading file " & strSrcFilename, vbCritical & vbOKOnly
    27.    
    28.     ReadFileAsBinary = False
    29.  
    30. End Function
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2
    Fanatic Member Bonker Gudd's Avatar
    Join Date
    Mar 2000
    Location
    Saturn
    Posts
    748

    Exclamation

    Integers overflow at 32000ish, use a Long.

  3. #3
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    Integer -32,768 to 32,767
    Long (long integer) -2,147,483,648 to 2,147,483,647


    the overflow is that the loops gone over 32,767 - try changing intSeekPos to lngSeekPos As Long

  4. #4

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    ok, now I get a Subscript out of range error on:

    VB Code:
    1. Get #intFileHandle, lngSeekPos, bytBuffer(lngSeekPos - 1)

    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    You're getting that error coz the byte array hasn't been dimensioned, or the (intSeekPos - 1) shoots past the array's bounds. Try:

    VB Code:
    1. Private Function ReadFileAsBinary(strSrcFilename As String, lngFileSize As Long, bytBuffer() As Byte) As Boolean
    2.  
    3.     Dim intFileHandle As Integer
    4.     Dim lngSeekPos As Long
    5.    
    6.     'On Error GoTo ErrHandler
    7.  
    8.     lngFileSize = FileLen(strSrcFilename)
    9.    
    10.     intFileHandle = FreeFile
    11.    
    12.     'Added by me :)
    13.     Erase bytBuffer
    14.     ReDim bytBuffer(1 To lngFileSize)
    15.  
    16.     Open strSrcFilename For Binary As intFileHandle
    17.  
    18.     For intSeekPos = 1 To lngFileSize
    19.         Get #intFileHandle, lngSeekPos, bytBuffer(lngSeekPos - 1)
    20.     Next intSeekPos
    21.    
    22.     Close intFileHandle
    23.  
    24.     ReadFileAsBinary = True
    25.  
    26.     Exit Function
    27.  
    28. ErrHandler:
    29.  
    30.     MsgBox "Error reading file " & strSrcFilename, vbCritical & vbOKOnly
    31.    
    32.     ReadFileAsBinary = False
    33.  
    34. End Function

  6. #6

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Error 10..

    The array is fixed or temorarily locked.

    Thanks for taking your time to help me axion_sa... but now I'm getting the above error and it's stopping on the ReDim statement.

    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  7. #7
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    i'd say that where u've dimensioned the array, it's something like...

    VB Code:
    1. Dim bytBuffer(1 To 10) 'Or the like
    2.  
    3. ''change the above Dim to...
    4. Dim bytBuffer()
    5.  
    6. ''and then call the function
    7. BooleanValue = ReadFileAsBinary("waka waka", 12345, bytBuffer())

  8. #8

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    hmm... ok, now we're back to the previous error, Subscript out of range on the Get statement.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  9. #9

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    anyone?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Here's your problem:
    VB Code:
    1. For intSeekPos = 1 To lngFileSize
    2.         'The first time through, lngSeekPos is 1......
    3.         Get #intFileHandle, lngSeekPos, bytBuffer(lngSeekPos - 1)
    4.         'The first time through, you try to use byt(Buffer( [lngSeekPos -1, which is 0]) ... BUT.....
    5.         'The array is defined as bytBuffer([B]1[/B] To lngFileSize)
    6.         'See the problem???
    7.         'Either the for is 0 to lngFileSize - 1 OR take the -1 off of the Get statement.
    8.         'BTW: why are you reading the file one byte at time, why not read the whole thing in at once, or in larger chunks?
    9.     Next intSeekPos
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    thanks techgnome. That worked perfectly

    To be honest, this is sample code from a project that does file transfer between a PC and a PocketPC. I'm not sure how file transfer works between the two, and maybe that is the reasoning behind reading it byte by byte.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

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