Link to home
Start Free TrialLog in
Avatar of cbueno
cbueno

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of cbueno
cbueno

ASKER

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

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
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.
Avatar of cbueno

ASKER

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

Avatar of cbueno

ASKER

NO sorry its ok!!!
I got it know!!

Thanks to all!!
Full points