why bad file error occur

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kirubagari
    New Member
    • Jun 2007
    • 158

    #1

    why bad file error occur

    [CODE=vb]Public Sub ReWrite_Open_Fi le(ByVal FileNo As Long)
    Dim FileSize As Long, Buffer() As Byte
    ' PART 1: Read the file.
    FileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
    ReDim Buffer(1 To FileSize) ' Set our buffer to that length.
    Get #FileNo, 1, Buffer() ' Grab the entire file's data into the array
    ' PART 2
    ' If you plan to change the data, this is where it would happen.
    'Beep

    ' PART 3: Write the file back.
    If MsgBox("Write data back to the file?", vbYesNo) = vbYes Then
    Put #FileNo, 1, Buffer() ' Write the data back to the file.
    End If
    End Sub



    Private Sub command2_click( )

    Dim mHandle
    Open "a:\bonding.bin " For Binary Access Read Write Lock Write As mHandle
    ReWrite_Open_Fi le mHandle

    End Sub[/CODE]this code give error bad file name or number.Why it give such error.when i put the break point and find out where the error the occur it occur in the line 1.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Looks like you have zero in variable mHandle.

    When you issue the Open statement, you have to supply the number of the "handle" to associate with that file. Generally, you should use the FreeFile function to get the next available number. For example...
    [CODE=vb]Private Sub command2_click( )
    Dim mHandle
    mHandle = FreeFile
    Open "a:\bonding.bin " For Binary Access Read Write Lock Write As mHandle
    ReWrite_Open_Fi le mHandle
    End Sub[/CODE]

    Caution: Once again this is merely demonstrating specific parts of the code. There are real problems with this code. For example, we are throwing away the value of mHandle when we exit the Click routine, but the file is still open. Big mistake!

    Comment

    Working...