Remembers Stream

Jay1b

Well-known member
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
I am having a problem with the code below. Firstly it tries to read the contents of the *hist file to an array. The *hist file should consist of ,,,, for a 100 lines, mostly with data inbetween the commas.

However after a powercut the file was filled with 200 nulls. This stopped the process from working, so i added a bit in the CATCH statement to delete the file.

Shortly later, the array is then written back to the *hist file (2nd procedure).

If i use a bad *hist file, it correctly deletes the file. But somehow, when i write back to the *hist file it remembers all the nulls and puts them in beforehand and i really cant see how. I even added another delete statement in the write procedure just to double check that the file is deleted, and streamwriter is set to overwrite any existing file anyway!

Any observations would be gratefully received.

Thanks.

Code:
    Read the ****.hist file to the array that fills the main flex grid
    Public Sub ReadHistory()

        Dim sr As New StreamReader(LogPath & ".hist")  Names the file too read    
        Dim strJobLine As String = sr.ReadLine
        Dim j As Integer = 0
        Dim k As Integer = 1
        Dim rx As New Regex(",")
        Dim aryHistory As String()

        Try
            Do While Not (strJobLine Is Nothing)
                aryHistory = rx.Split(strJobLine)  splits the string every , 
                               putting them into, there own entry in an array
                If k < 5 Or k > 95 Then MsgBox(Str(k))  test line
                While j <= JMax
                    aryTenRecent(j, k) = aryHistory(j)
                    j += 1
                End While
                j = 0
                k += 1 still needed to increment the bit where it actually fills the array
                strJobLine = sr.ReadLine
            Loop
            sr.Close()
        Catch
            Try
                sr.Close()
                File.Delete(LogPath & ".hist")
            Catch
                MsgBox(Err.Description)
            End Try


              File.Delete(LogPath & ".hist")
            LogOutput("Error with the ReadHistory Function.  " + Err.Description)
             MsgBox(Err.Description)
        End Try
        sr.Close()
    End Sub

Code:
   Write the contents to a file *****.hist from the array that fills the main flex grid
    Public Sub WriteHistory()

        Try
            If File.Exists(LogPath & ".hist") Then _
                 File.Move(LogPath & ".hist", LogPath & "_old.hist")
            Dim sw As New StreamWriter((LogPath & ".hist"), False) Names the file 2write
            Dim j As Integer = 0
            Dim k As Integer = 1
            Dim strJobLine As String

            This adds all the data for each line at a time to a file.
            While k <= MainFlexSize Counts from 1 to mainflexsize (100)
                While j <= JMax Counts from 0 to JMax (4)
                    strJobLine = strJobLine + aryTenRecent(j, k) + ","
                    j += 1
                    sw.WriteLine("#1 STRJOBLINE = " & strJobLine) testdata
                End While
                j = 0
                k += 1
                strJobLine = strJobLine.Remove(strJobLine.Length - 1, 1)
                sw.WriteLine(strJobLine)
                strJobLine = ""
            End While
            sw.WriteLine("-------------------Closed-------------------") testdata
            sw.Close()
        Catch
             MsgBox(Err.Description)
            LogOutput("Error with the WriteHistory Function. " + Err.Description)
        End Try

    End Sub
 
you need to add , false to overwrite the file , or , true to append to the file , eg:
Code:
Dim sr As New StreamReader(LogPath & ".hist" , False) /// overwrite the existing file by adding False.
 
Cheers, but I have

Code:
       Dim sw As New StreamWriter((LogPath & ".hist"), False)

Which is where the file is written, your saying i need it when the file is being read? WHY?
 
i was seeing StreamReader instead of StreamWriter :-\ my eyes must be playing up with all the sleeplessness.
is the file read-only , or being used elsewhere? because if its anything like the Index.Dat files for IE history
you can delete the nulls and click save , but when you re-open the file the nulls are back again.
i presume youve tried a Replace( " null chars " , "" ) ?
 
No i havent tried the replace command, didnt know about it to be honest.

The file isnt being used anywhere else, and due to desparation it is now deleted TWICE before streamreader tries to write back out to it, using overwrite.

Been thinking about this and tomorrow i will try to read it from that file, and then write it out to another file. This should hopefully tell me whether its adding to current file, or writing the NULLs in somehow into the new file. If that fails i shall set it to replace all the NULLs with a space or something.
 
SOLVED!

The problem was not the file itself, that was being deleted correctly. Before it dropped out of the read procedure, it read some of the nulls into the arytenrecent array. I added

Code:
   aryTenRecent.Clear(aryTenRecent, 0, aryTenRecent.Length)

To clear the array in the catch statement of the read procedure, and that works perfectly.

Incidentally is there a better way of clearing the array? I cant see why i need to name the array twice, such as

array.clear(array, start, end)

I would of thought the array.clear would explain what array i wanted to clear!

Thanks for you help Dynamic :)
 
Back
Top