Writting binary files. Easy question

Hello to all

Hi i have been traying to write to a file in binary but everytime i open the file is in text mode rather than in binary!! Can anyone supply me with a working code on how to do this??? Reading from it would be usefull too!!

Carlos Bueno
carlosbueno2000@yahoo.com
cbuenoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arthur_WoodCommented:
show the code that you are trying to use, please.  Then maybe we can assist you.

AW
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulHewsCommented:
Option Explicit

Private Sub Command1_Click()
    Dim lngTest As Long
    Dim bytTest(1 To 4) As Byte
    Dim i As Integer
    Dim hFile As Integer
   
    lngTest = 2
    For i = 1 To 4
        bytTest(i) = i * lngTest
    Next i
    'write out to a binary file
    hFile = FreeFile
    Open "C:\temp\test.dat" For Binary As #hFile
    Put #hFile, , lngTest
    Put #hFile, , bytTest
    Close hFile
End Sub

Private Sub Command2_Click()
    Dim lngTest As Long
    Dim bytTest(1 To 4) As Byte
    Dim i As Integer
    Dim hFile As Integer
   
   'read the binary file into variables
    hFile = FreeFile
    Open "C:\temp\test.dat" For Binary As #hFile
    Get #hFile, , lngTest
    Get #hFile, , bytTest
    Close hFile
   
    For i = 1 To 4
        Debug.Print bytTest(i)
    Next i
    Debug.Print lngTest
   
   
End Sub
0
cbuenoAuthor Commented:
Hi AW

Well all i want to write to the file is something like
Var1="START" .

Now when i read if i find the Var1="START" then write
Var1="CLOSE"

Here is the code:
---------------------
Private Sub Command1_Click()
Dim var1 As String
var1 = "World"
Open "c:\scr.dat" For Binary Access Write As #1
Put #1, , var1
Close #1
MsgBox "Writing: Hello " & var1
End Sub

Private Sub Command2_Click()
Dim var1 As String
Open "c:\scr.dat" For Binary Access Read As #1
Get #1, , var1
Close #1
MsgBox "Reading: Hello " & var1
End Sub
------------
But when i open the file its in text mode rather than in binary!!

Carlos Bueno

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Arthur_WoodCommented:
one problem:

a String variable is by default of ZERO length, and thus when you read from the file, that is what you will read (a zero-length string:

change your Command2 code (here you create a maximum String BUFFER of 10 characters)

what is it that you EXPECT the code to do, as you are in fact storing and retrieving STRINGS (and NOT character arrays).


Private Sub Command2_Click()
Dim var1 As String
Open "c:\scr.dat" For Binary Access Read As #1
var1 = String(10," ")
Get #1, , var1
Close #1
MsgBox "Reading: Hello " & var1
End Sub
0
PaulHewsCommented:
You can change the Command2 code like this so that it will work properly:

Private Sub Command2_Click()
Dim var1 As String
Open "c:\scr.dat" For Binary Access Read As #1
var1 = Space(LOF(1))  'create a string long enough to hold the contents of scr.dat file
Get #1, , var1
Close #1
MsgBox "Reading: Hello " & var1
End Sub


You see the Get command will retrieve as much binary data from the file as will fit in the variable you pass it.  If you pass a string, you have to resize the string (using Space or STring function) to the lenght of the string you want to read from the file.
0
cbuenoAuthor Commented:
But there is still a small problem!!
In the Commman2_Click when displaying the info in the message box. The variable (var1) been read from the file is not displayed in the message box!!
MsgBox "Reading: Hello " & var1

0
cbuenoAuthor Commented:
NO sorry its ok!!!
I got it know!!

Thanks to all!!
Full points
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.