Link to home
Start Free TrialLog in
Avatar of khacharn
khacharn

asked on

How to read a binary file..

hi all i want to know how to read a binary file in VB..
Please help ASAP
Regards
Nitin
Avatar of Ark
Ark
Flag of Russian Federation image

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
Avatar of Vbmaster
Vbmaster

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.
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
Avatar of khacharn

ASKER

Adjusted points to 30
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

Adjusted points to 40
ASKER CERTIFIED SOLUTION
Avatar of Vbmaster
Vbmaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx Mr. Vbmaster ... ur code was indeed of great help
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
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?)
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
"//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.