Solved

Cannot get file contents

Posted on 2014-11-03
4
98 Views
Last Modified: 2014-11-03
I am having trouble getting the contents of a binary file
The file is saving fine, but it is empty for some reason
What is wrong ?
Public Sub SaveFile(txt As String)
Dim FileName As String
Dim FF As Integer
FF = FreeFile
FileName = GetPathToAppFolder & "\bWin64.dll"
Debug.Print FileName
Open FileName For Binary As #FF
     Put #FF, 1, txt 'Data
    Close #FF
End Sub

Public Function CheckFileContents() As Boolean
Dim txt As String
Dim FileName As String
Dim FF As Integer
FF = FreeFile
FileName = GetPathToAppFolder & "\bWin64.dll"
Debug.Print FileName
If FileExists(FileName) Then
MsgBox "yes"
End If
       Open FileName For Binary As #FF
          Get #FF, , txt
      Close #FF
      Debug.Print "txt " & txt
   
       Exit Function

End Function

Open in new window

0
Comment
Question by:isnoend2001
  • 2
  • 2
4 Comments
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
you can't print binary data as text as the first binary zero character would terminate the output.

you need to print the data byte by byte for example as hex characters by using the Hex function.

Sara
0
 

Author Comment

by:isnoend2001
Comment Utility
Thanks sarabande, but i don't know what you mean i have other save/opens that work just fine
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
but i don't know what you mean
if you open a binary file using a hex editor, you encounter bytes with value (code) from 0 to 255 (0x00 to 0xFF in hex notation).

not all of these codes are printable characters (see ascii and ansi chart tables). for example codes from 0 to 31 decimal are not printable and code 32 is a space character.

you can assign non-printable characters to a string, or read them from binary file, but when printing them you may see "odd" characters or nothing. the latter would happen if the string contains a binary zero character (or binary zero wide character if UNICODE). the string output would be terminated at the first zero character.

i have other save/opens that work just fine
if the files contain text, it could be fine.

you wrote text to the file but have it opened as binary. because of that the following applies:

For files opened in Binary mode, all of the Random rules apply, except:
•The Len clause in the Open statement has no effect. Put writes all variables to disk contiguously; that is, with no padding between records.
•For any array other than an array in a user-defined type, Put writes only the data. No descriptor is written.
•Put writes variable-length strings that are not elements of user-defined types without the 2-byte length descriptor. The number of bytes written equals the number of characters in the string.

because of that the string written is not length terminated (neither by descriptor nor by zero termination) and probably could not properly read by Get because of that.

if you want to read a text file you should write a text file and not a binary file or read a text file created by a text editor. if you want to read binary data you should read them byte by byte or into a byte array.

Sara
0
 

Author Closing Comment

by:isnoend2001
Comment Utility
Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now