Cannot get file contents

Posted on 2014-11-03
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

Question by:isnoend2001
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 34

Expert Comment

ID: 40419836
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.


Author Comment

ID: 40419875
Thanks sarabande, but i don't know what you mean i have other save/opens that work just fine
LVL 34

Accepted Solution

sarabande earned 500 total points
ID: 40419979
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.


Author Closing Comment

ID: 40419992

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

718 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