Solved

open as binary

Posted on 2000-04-22
10
266 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 30 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now