Results 1 to 5 of 5

Thread: File IO use to be so easy (resolved)

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member Foxer's Avatar
    Join Date
    Oct 2001
    Location
    Australia
    Posts
    278

    File IO use to be so easy (resolved)

    I have a flat text file that has some weird control characters so I'm reading this in one character at a time, filtering out the special characters, inserting line breaks so I can read it, and then outputing to a text file.



    Code:
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'Open text file for reading
            Dim SRead As Stream
            SRead = File.OpenRead("C:\Documents and Settings\campbega\Desktop\comments1.db")
            Dim SrRead As StreamReader = New StreamReader(SRead, System.Text.Encoding.ASCII)
    
            ' set the file pointer to the beginning
            SrRead.BaseStream.Seek(0, SeekOrigin.Begin)
            SrRead.BaseStream.Position = 0
    
    
            'Kill existing output file
            File.Delete("c:\McGinty.txt")
    
            'Create new output file
            Dim sw As StreamWriter
            sw = File.CreateText("c:\McGinty.txt")
    
            Dim buffer(1) As Char
    
            While (SrRead.BaseStream.Position < SrRead.BaseStream.Length)
                
                'Read a single character
                SrRead.Read(buffer, 0, 1)
    
                'Test character for weird stuff
                Select Case Asc(buffer(0))
                    Case 33 To 89  'alpha characters up to "Z"
                        sw.Write(buffer(0))
                    Case (90)  'this is the letter Z - demarkation for a new line
                        sw.WriteLine()
                    Case 91 To 122  'This is the small alphabet a -> z
                        sw.Write(buffer(0))
                    Case Else  'Anything outside this gets replaced with a space
                        sw.Write(" ")
                End Select
    
                'Move the buffer to the next character
                SrRead.BaseStream.Position = SrRead.BaseStream.Position + 1
    
                'The good old fashioned DoEvents
                System.Windows.Forms.Application.DoEvents()
    
            End While
    
            'Do the normal file IO clean up
            SrRead.DiscardBufferedData()
            SrRead.Close()
    
            sw.Flush()
            sw.Close()
    
            End
        End Sub
    This works fine. The problem is that my input file is 1.6 meg long and all characters. It's obviously a lot but big deal right?

    When i output this character by character, my output file stops somewhere in the middle so my output is shorter than my input file and thus incomplete. It chops off the end of my input text.

    As a test, I deleted about 1000 characters off the beginning of my input file and wow - my output file "progressed" 1000 characters into the file before getting chopped off.

    Is there a buffer limit or something like that I'm exceeding with the code above? Something is truncating somewhere and I've no idea where/why.

    I'd provide the input file as well except it has confidential insurance date.[/COLOR]
    Last edited by Foxer; Mar 16th, 2004 at 10:57 PM.
    Rate my response if I helped

    Go Hard Or Go Home


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width