I am converting an old VB5 program to
VB 2005. The program processes some 4,000 Metastock files. It reads through an index, gets the full path name to read the F999.DAT file header in order to get a stockâs record count. Then it reads and processes the given number of price records for each stock and outputs data to a couple of new files.
The first record was processed and fileclose(2) without any problem but opening all subsequent records generates the error message âA first chance exception of type 'System.IO.EndOfStreamException' occurred in Microsoft.VisualBasic.dllâ on the line
FileGet(2, fdathd, 1) ' read stock file - header record
The debugger lists âUnable to read beyond the end of the stream. Check whether the end of the file has been reached before reading.â
So I tried a couple of things - see comments below but it still appears that having done a fileclose(2) I cannot re-open the same stream fileopen(2) with my next file. To me it seems the stream resources are not being released properly. What am I missing? Please advise. Thank you.
Here is the DIM format of the stock:
Public Structure fdathdtype ' stock header record
Dim dmaxrec As Short
Dim dtotrecs As Short ' total number of price records
<VBFixedString(24)> Dim dfill1() As Byte
End Structure
Public fdathd As fdathdtype
Public Structure fdattype ' stock detail price record
Dim ddate As Single
Dim dopen As Single
Dim dhigh As Single
Dim dlow As Single
Dim dclose As Single
Dim dvol As Single
Dim dopenint As Single
End Structure
Public fdat As fdattype
Here is the problem code:
Sub Read_master_recs() ' READ STOCK DETAILS
fdatname = DirRoot & "\" & FileName â stock filename with full path
Debug.Print(Stkcode & "=code " & Stkname & "=stkname " & DirRoot)
' Secondly I opened and closed the filestream at position zero to ensure I was at start of stream file(2)- but Iâm still getting the endofstreamexception error.
Dim rd As Stream = File.OpenRead(fdatname)
rd.Position = 0
rd.Close()
FileOpen(2, fdatname, OpenMode.Random, OpenAccess.ReadWrite, OpenShare.Shared, 28) ' OPEN STOCK RECORD eg C:\10YR\ED\F999.DAT
recnt = 1
' Firstly I put in an EOF test to ensure I was at start of file, after having opened file(2) - the program did not STOP so I must be at start of file.
If EOF(2) = True Then Stop '
FileGet(2, fdathd, 1) ' read header record PROBLEM THIS LINE
p = fdathd.dtotrecs - 1 ' no of stock recs/bars
ReDim acl(p) ' set array size dynamically , format = "double"
ReDim aop(p)
ReDim ahi(p)
ReDim alo(p)
ReDim avol(p)
ReDim adate(p)
' extract & store prices
Do While p >= recnt â this is EOF test
recnt = recnt + 1
FileGet(2, fdat, recnt) ' read detail stock prices
â¦. More code
Data written to fileopen(3) and fileopen(4) etc
â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦..
Loop
Fileclose(2)
Return
Exit Sub
End Sub