|
-
Aug 8th, 2002, 09:56 AM
#1
Thread Starter
Frenzied Member
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:
Private Function ReadFileAsBinary(strSrcFilename As String, lngFileSize As Long, bytBuffer() As Byte) As Boolean
Dim intFileHandle As Integer
Dim intSeekPos As Integer
'On Error GoTo ErrHandler
lngFileSize = FileLen(strSrcFilename)
intFileHandle = FreeFile
Open strSrcFilename For Binary As intFileHandle
[COLOR=RED]For intSeekPos = 1 To lngFileSize[/COLOR]
Get #intFileHandle, intSeekPos, bytBuffer(intSeekPos - 1)
Next intSeekPos
Close intFileHandle
ReadFileAsBinary = True
Exit Function
ErrHandler:
MsgBox "Error reading file " & strSrcFilename, vbCritical & vbOKOnly
ReadFileAsBinary = False
End Function
-
Aug 8th, 2002, 10:00 AM
#2
Fanatic Member
Integers overflow at 32000ish, use a Long.
-
Aug 8th, 2002, 10:00 AM
#3
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
-
Aug 8th, 2002, 10:08 AM
#4
Thread Starter
Frenzied Member
ok, now I get a Subscript out of range error on:
VB Code:
Get #intFileHandle, lngSeekPos, bytBuffer(lngSeekPos - 1)
-
Aug 8th, 2002, 10:20 AM
#5
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:
Private Function ReadFileAsBinary(strSrcFilename As String, lngFileSize As Long, bytBuffer() As Byte) As Boolean
Dim intFileHandle As Integer
Dim lngSeekPos As Long
'On Error GoTo ErrHandler
lngFileSize = FileLen(strSrcFilename)
intFileHandle = FreeFile
'Added by me :)
Erase bytBuffer
ReDim bytBuffer(1 To lngFileSize)
Open strSrcFilename For Binary As intFileHandle
For intSeekPos = 1 To lngFileSize
Get #intFileHandle, lngSeekPos, bytBuffer(lngSeekPos - 1)
Next intSeekPos
Close intFileHandle
ReadFileAsBinary = True
Exit Function
ErrHandler:
MsgBox "Error reading file " & strSrcFilename, vbCritical & vbOKOnly
ReadFileAsBinary = False
End Function
-
Aug 8th, 2002, 10:28 AM
#6
Thread Starter
Frenzied Member
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.
-
Aug 8th, 2002, 10:31 AM
#7
i'd say that where u've dimensioned the array, it's something like...
VB Code:
Dim bytBuffer(1 To 10) 'Or the like
''change the above Dim to...
Dim bytBuffer()
''and then call the function
BooleanValue = ReadFileAsBinary("waka waka", 12345, bytBuffer())
-
Aug 8th, 2002, 10:38 AM
#8
Thread Starter
Frenzied Member
hmm... ok, now we're back to the previous error, Subscript out of range on the Get statement.
-
Aug 8th, 2002, 11:23 AM
#9
Thread Starter
Frenzied Member
-
Aug 8th, 2002, 12:10 PM
#10
Here's your problem:
VB Code:
For intSeekPos = 1 To lngFileSize
'The first time through, lngSeekPos is 1......
Get #intFileHandle, lngSeekPos, bytBuffer(lngSeekPos - 1)
'The first time through, you try to use byt(Buffer( [lngSeekPos -1, which is 0]) ... BUT.....
'The array is defined as bytBuffer([B]1[/B] To lngFileSize)
'See the problem???
'Either the for is 0 to lngFileSize - 1 OR take the -1 off of the Get statement.
'BTW: why are you reading the file one byte at time, why not read the whole thing in at once, or in larger chunks?
Next intSeekPos
-
Aug 8th, 2002, 12:20 PM
#11
Thread Starter
Frenzied Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|