Cannot get file contents

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

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 35

Accepted Solution

sarabande earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

752 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