Solved

How to read a binary file..

Posted on 2000-02-25
12
295 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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: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 40 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

792 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