Solved

open as binary

Posted on 2000-04-22
10
267 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Controlling which port to download from 4 71
VB6 - Convert HH:MM into Decimal 8 51
Help me. 3 46
fso.FolderExists("\\server\HiddenFolder$") 4 51
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

920 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

12 Experts available now in Live!

Get 1:1 Help Now