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

x
?
Solved

How to read a binary file..

Posted on 2000-02-25
12
Medium Priority
?
301 Views
Last Modified: 2010-05-02
hi all i want to know how to read a binary file in VB..
Please help ASAP
Regards
Nitin
0
Comment
Question by:khacharn
  • 6
  • 5
12 Comments
 
LVL 28

Expert Comment

by:Ark
ID: 2560545
Hi
Dim b() as Byte
Open "c:\myfile" for binary as #1
Redim b(LOF(1))
for i=1 to LOF(1)
Get #1,,b(i)
Next i
Close #1
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2560976
Actually Ark's code is really *slow*, the correct way of reading that byte array would be...

Dim b() as Byte

Open "c:\myfile" for binary as #1
Redim b(LOF(1))
Get #1,,b()
Close #1

Check your helpfile for Get and Put for more example code.
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2560982
This was embarrassing, the right code should of course read like...

Dim b() as Byte

Open "c:\myfile" for binary as #1
If (LOF(1) > 0) Then                            '<- a error will occur if you try
                                                           ' to read a empty file, avoid
   ReDim b(0 To LOF(1) - 1)                  '<- changed
   Get #1,,b()
End If
Close #1
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:khacharn
ID: 2573666
Adjusted points to 30
0
 

Author Comment

by:khacharn
ID: 2573667
hi guys..
i am still not clear with this BINARY parsing..why did god create PARSING..

Well...back to business..
I have this Data Structure which come to me in the Binary file i was talking about..
---------------------------------------
DATA STRUCTURE..
Transaction Code short (2 Bytes)
Timestamp        long (4 Bytes)
Message Length        short (2 Bytes)
---------------------------------------
The records which come in the binary file have the above Datastructure..

Can anyone of you provide me with CODE TO DO THIS>>
Please help..its URGENT..(i will bargain more points..for it..)
Regards
Nitin

0
 

Author Comment

by:khacharn
ID: 2573673
Adjusted points to 40
0
 
LVL 7

Accepted Solution

by:
Vbmaster earned 160 total points
ID: 2573939
You can define a UDT (userdefined type) in a module or in the declarations part of a form (replace Public to Private if you define it in a form).

  Public Type DataStructure
    Code As Integer
    TimeStamp As Long
    Length As Integer
  End Type

Then you can read from the binary file using code like...

  Dim Item As DataStructure

  Open "C:\Aapp.txt" For Random As #1 Len = Len(Item)
  Get #1, recordnumbertoread, Item
  Get #1, recordnumbertoread, Item
  Get #1, recordnumbertoread, Item
  Close #1
0
 

Author Comment

by:khacharn
ID: 2575891
Thanx Mr. Vbmaster ... ur code was indeed of great help
0
 

Author Comment

by:khacharn
ID: 3197854
VBMASTER I need some help ..again on Binary files..Please help
I have this structure..cna you tell me how to create a private type for it..
The structure is :
----------------------------------------
short iToken;                   //2
char cSymbol[10];//10
CHAR cSeries[2];//2
DOUBLE dIssuedCapital; //8
SHORT iWarningPercent;//2
SHORT iFreezePercent;//2
CHAR cCreditRating[12];//12
SHORT iIssueRate;//2
LONG lIssueStartDate;//4
----------------------------------------
It would help me a lot if you could Resopnd asap
I am fumbling with the char how to decalre it in a private type
Please help asap
Regards
khacharn
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 3200714
I'm a little confused by your //numbers to the right of the lines, looks loke you are defining the length of the respective line, but isn't

CHAR cSeries[2] defining a string with the length 3 (starts at index 0) and not //2 as the end-of-the-line-number states?

You can define these CHAR sequences as String*3 (cSeries[2]), String*13 (cCreditRating[12]) and String*11 (cSeries[10]).

SHORT -> Integer (2 bytes)
LONG -> Long (4 bytes)
DOUBLE -> Double? (8 bytes - don't know if this is right, is the mantissa size the same in C as in VB?)
0
 

Author Comment

by:khacharn
ID: 3201451
VB master thanx for all the help..
I have solved the problem..
//2 is just a comment saying what the lenth of the field is(in bytes)
Regards
khacharn
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 3202412
"//2 is just a comment saying what the lenth of the field is(in bytes)"

this was what made me confused, since CHAR cSeries[2] has a length of 3 bytes and not 2 bytes (aint all arrays in C(++) 0-based?). ;)

But you got it working anyway. Great.
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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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.
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

926 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