Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to read a binary file..

Posted on 2000-02-25
12
Medium Priority
?
300 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

722 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