?
Solved

Writting binary files. Easy question

Posted on 2003-03-25
7
Medium Priority
?
138 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:cbueno
[X]
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
  • 3
  • 2
  • 2
7 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 320 total points
ID: 8203342
show the code that you are trying to use, please.  Then maybe we can assist you.

AW
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8203391
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
 

Author Comment

by:cbueno
ID: 8203450
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8203760
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 8203776
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
 

Author Comment

by:cbueno
ID: 8203917
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
 

Author Comment

by:cbueno
ID: 8203946
NO sorry its ok!!!
I got it know!!

Thanks to all!!
Full points
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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 Month12 days, 17 hours left to enroll

777 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