Results 1 to 3 of 3

Thread: Opening and reading binary files

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Manchester, UK
    Posts
    50
    I am getting strange errors when opening files for binary reading. Here is my code:

    fstr = ""
    Open "c:\misc16.fsh" For Binary Access Read As #1
    Do While Not EOF(1)
    Get #1, temp
    fstr = fstr & temp
    Loop

    When the code reaches the 'Get' line I get the following error:

    Runtime error '458':

    Variable uses an automation type not supported in Visual Basic

    I am using VB5 Enterprise SP3 and this code worked before I installed SP3. Please help!

  2. #2
    Lively Member
    Join Date
    Apr 2000
    Posts
    110

    Wink This may help...

    If you are opening the file for binary access, then you can only retrieve chunks of data from the file, specified by the length of the string you declare. e.g.

    Code:
    Example 1:
         Open "C:\Test.txt" for binary as #1
         'Get all data in the file
         strTemp = Space(LOF(1))
         Get #1, , strTemp
         Close #1
    
    Example 2:
         
         Dim strTemp as string * 10  '10 bytes
    
         Open "C:\Test.txt" for binary as #1
         
         'Get 1- bytes from start of file.
         Get #1, , strTemp
         Close #1
    If you want to retrieve data from each line in your file (which it looks like you are trying to do), then you should use the following method.

    Code:
    'To read lines from a file, and append them
    'to a string (with carriage returns at the end)
    'use this method.
    
    Open "C:\Test.txt" For Input As #1
    
    'Loop while not the end of file
    Do While Not EOF(1)
        
        'Get a line form the file
        Input #1, strTemp1
    
        'Append it to the file
        strTemp2 = strTemp2 & strTemp1 & vbCrLf
    
    Loop
    Or, if you are just after getting a big chunk from the file, use the following method.

    Code:
    'To retrieve a whole chunk from a file, use this method.
    
    Open "C:\Test.txt" For Binary As #1
        
        'Create a string buffer
        strTemp1 = Space(LOF(1))
        
        'Get the data from the file
       Get #1, , strTemp1
    
    Close #1
    I hope this has helped. If you are still having problems, post back and explain in more depth what you are exactly trying to retrieve from the file...

    Laterz

    REM





  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Manchester, UK
    Posts
    50

    Cool Thanks

    Cheers, that really helped.

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