Solved

Cannot get file contents

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

Expert Comment

by:sarabande
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.

Sara
0
 

Author Comment

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

Accepted Solution

by:
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.

Sara
0
 

Author Closing Comment

by:isnoend2001
ID: 40419992
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

919 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

14 Experts available now in Live!

Get 1:1 Help Now