Solved

open as binary

Posted on 2000-04-22
10
272 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
[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
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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
Course of the Month5 days, 1 hour left to enroll

635 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