Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

open as binary

Posted on 2000-04-22
10
Medium Priority
?
275 Views
Last Modified: 2010-05-18
Hello, is there any ppl tell me that what is the use of open as binary ?? And can give me an complete and simplest example of how to use this ??
Thanks in advance !
0
Comment
Question by:ahchong
10 Comments
 
LVL 9

Expert Comment

by:Ruchi
ID: 2740437
The binary files are accessed byte by byte.

'Fill the string variable MyString with the string "This is a test"
MyString = "This is a test"

'Get a free file number
FileNum = FreeFile

'Open the file Test.Txt for binary access
Open "Test.txt" For Binary As FileNum

'Write the sstring variable MyString, starting at byte location 100
Put #FileNum, 100, MyString

'close the file
Close FileNum
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2740440
When you open a file for binary access , you can read from and write to any byte location in the file.

Hope this helps.
0
 
LVL 9

Accepted Solution

by:
Ruchi earned 120 total points
ID: 2740496
This is a very small program. Place a command button on the form.

Option Explicit

Private Sub Command1_Click()

Dim MyString As String
Dim FileNum As Integer
'Fill the string variable MyString with the string "This is a test"
MyString = "This is a test"

'Get a free file number
FileNum = FreeFile

'Open the file Test.Txt for binary access
Open "C:\Test1.txt" For Binary As FileNum

'Write the sstring variable MyString, starting at byte location 100
Put #FileNum, 100, MyString

'close the file
Close FileNum
End Sub

You can go to http://www.vbi.org/Library/Novice/Intro/Doing%20File%20IO/Section4.html
0
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!

 

Author Comment

by:ahchong
ID: 2740893
yeah ~~, thanks !! but what is actually freefile, why we should use it ?
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2741062
The FreeFile function returns the next available filenumber (it's not used at the time anywhere else in your program). This is needed because if you try to open two files with the same filenumber at the same time you will get a error. But all this is probably already stated in the helpfile...
0
 

Expert Comment

by:dtselva
ID: 2741085
there's 2 types of mode that can be used to open a file.
1. Sequential
2. Binary

When u open a sequential file, the data is read line by line. Meaning, no fixed length and thus, you can't move the file pointer to anywhere in the file but when it comes to binary format, it's something like a database, the length of each record is fixed thus u can pinpoint the exact location of the file but when you open this file with a Notepad.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2741136
The FreeFile function returns the next available file number value. For example, if you have used #1 and #2 for open files, the next value returned from FreeFile () will be 3. FreeFile () is most helpful when you write the procedures that need to open files, and the procedures may be called from more than one place in an appolication. Now matter how many files are open, the procedure will always use the next file number ... to open its file.

If you want to put the #1
Open "Test.txt" For Binary As #1

That FileNumber (#1, #2, #3, ...) represents a number from 1 to 255 and assocaites the open file with that number. If you open and close multiple files throughout the application, using that #1 might not be a good idea.

Hope this helps you a lot?
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2741142
Oops.. Now matter how -- No matter how. :)
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2741148
As dtselva is new to EE.. I very graciously extend our welcome. I also would like to point out, that most experts here use the answer button very frugally, and then only when they are absolutely certain that their response could be the ONLY possible answer. dtselva, I think you will soon find, that if your answer is most appropriate to the questioners need, you will almost always receive the exert points your comment merits. Once again, welcome aboard dtselva.. I think you will enjoy your time immensely.. and if you can.. try and go lightly on the answer button.. and heavy on the comment button (as most experts do).. until you become more comfortable.. <smile>.


0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2754254
Are you satisfied with the comments posted by us? See "Accept as answer" button at the right of the Comment.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

916 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