Solved

How to read a binary file..

Posted on 2000-02-25
12
292 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 27

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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

706 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

16 Experts available now in Live!

Get 1:1 Help Now