Click here to Skip to main content
15,918,168 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How can I read binary file ".DAT" to struct?

In my vba code:
Option Explicit
Type Security_File
    StockNo As Integer
    StockSymbol As String * 8
    StockType As String * 1
    Ceiling As Long
    Floor As Long
    BigLotValue As Double
    SecurityName As String * 25
    SectorNo As String * 1
    Designated As String * 1
    SUSPENSION As String * 1
    Delist As String * 1
    HaltResumeFlag As String * 1
    SPLIT As String * 1
    Benefit As String * 1
    Meeting As String * 1
    Notice As String * 1
    ClientIDRequired As String * 1
    CouponRate As Integer
    IssueDate As String * 6
    MatureDate As String * 6
    AvrPrice As Long
    ParValue As Integer
    SDCFlag As String * 1
    PriorClosePrice As Long
    PriorCloseDate As String * 6
    ProjectOpen As Long
    OpenPrice As Long
    Last As Long
    LastVol As Long
    LastVal As Double
    Highest As Long
    Lowest As Long
    Totalshares As Double
    TotalValue As Double
    AccumulateDeal As Integer
    BigDeal As Integer
    BigVolume As Long
    BigValue As Double
    OddDeal As Integer
    OddVolume As Long
    OddValue As Double
    Best1Bid As Long
    Best1BidVolume As Long
    Best2Bid As Long
    Best2BidVolume As Long
    Best3Bid As Long
    Best3BidVolume As Long
    Best1Offer As Long
    Best1OfferVolume As Long
    Best2Offer As Long
    Best2OfferVolume As Long
    Best3Offer As Long
    Best3OfferVolume As Long
    BoardLost As Integer
End Type

Code read file:
Private Sub ReadSecurity()
    Dim strFileName As String
    Dim intFileNum As Integer
    Dim strFileNameMar As String
    Dim intFileNumMar As Integer
    Dim record As Security_File
    Dim intRecordNum As Integer
    strFileName = "E:\Documents\MyData.DAT"
    intFileNum = FreeFile
    intRecordNum = 1
    Open strFileName For Random Shared As intFileNum Len = Len(record)
    While Not EOF(intFileNum)
        Get intFileNum, intRecordNum, record
        With record
         If .StockType = "S" Or InStr(1, StrCodeStockView, Replace(Trim(.StockSymbol), "'", "")) <> 0 Then
                 MsgBox (.StockType )
       End If
      End With
        intRecordNum = intRecordNum + 1
    Close #intFileNum
 I don't know how to convert vb to c #.
End Sub

What I have tried:

I tried it but the data is not correct.
My c# code:
using (BinaryReader b = new BinaryReader(File.OpenRead(@"E:\Documents\MyData.DAT")))

               for (int i = 0; i < b.BaseStream.Length; ++i)
                       var a1 = b.ReadString();
                       var t1 = b.ReadInt64();
                       var a2 = b.ReadSingle();
                       var a3 = b.ReadSingle();
                       var a4 = b.ReadSingle();
                       var a5 = b.ReadSingle();
                       var a6 = b.ReadSingle();
                       var a7 = b.ReadSingle();
                       var a8 = b.ReadSingle();
                       var a9 = b.ReadSingle();
                       //streamWriter.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", a1, a2, a3, a4, a5, a6, a7, a8, a9, t1));
                       Console.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", a1, a2, a3, a4, a5, a6, a7, a8, a9, t1));
Updated 6-Apr-23 8:36am
BillWoodruff 15-Oct-20 10:28am    
How did you serialize the data in VB: show that code.
tvtoanitvn 15-Oct-20 21:21pm    
I get the data automatically. In the way of old software.
Link my file:
BillWoodruff 16-Oct-20 3:42am    
I think you will find that we are reluctant to open an unknown file for security reasons. Add more specific information to your question.

1 solution

The first element in your VBA type is an Integer. The first element you try to read in your C# code is a String.

Also bear in mind in VBA, Integer is a 16-bit type, and Long is a 32-bit type. They map to Int16 and Int32 (or short and int) in C#.
Share this answer
tvtoanitvn 15-Oct-20 5:13am    
Richard Deeming. After sorting the location as vba code. The data is still inaccurate.
tvtoanitvn 15-Oct-20 5:18am    
Link my file:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900