Solved

open as binary

Posted on 2000-04-22
10
270 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 40
vb6 connector to mongodb 2 122
Create Files based on Cell Values in a Range in Excel 12 43
Send outlook email from VBS Script 2 45
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

821 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