Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 12th, 2007, 08:22 AM
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Fileclose() does not appear to reset stream? EndOf

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


 
Old February 22nd, 2007, 05:44 PM
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

After much experimenting here is my solution. There is a compiler timing fault. As the compiler closes down the old fileclose(2) stream, it is not checking the end of the close thread. Therefore a new fileopen(2) command sometimes gets the EndofStreamexception error. The fault is consistent stopping at the same file but not at others.

My workaround is to use:
Dim fs As New FileStream(fdatname, FileMode.Open, FileAccess.Read, FileShare.Read) .... etc in new VB2005 language

otherwise you will have to do an error trap and repeat fileopen(2) - perhaps you might want to enter a millisecond pause delay too.

rgs






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to reset the form? vinaykurudi Apache Tomcat 0 September 29th, 2008 05:26 AM
how to reset the SessionID? akkad C# 2 October 24th, 2007 06:49 AM
BackgroundImage reset ryan.thomson VB.NET 2002/2003 Basics 0 October 25th, 2005 08:39 AM
how 2 reset a form kanoorani Javascript How-To 6 October 10th, 2005 05:45 AM
reset password help nazra Classic ASP Databases 3 July 25th, 2003 09:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.