How can I read binary file ".DAT" to struct?
In my vba code:
Struct:
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
Wend
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)
{
try
{
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();
Console.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("===================");
}
catch
{
continue;
}
}
}