Results 1 to 5 of 5

Thread: StreamReader Help...

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2002
    Location
    Oklahoma City, OK
    Posts
    3

    StreamReader Help...

    I'm not sure the best way to perform this task. I'm trying to read through a text file, parsing out certain lines. However, my Do...While loop looks for the StreamReader.ReadLine <> Nothing when it closes the loop.

    The text files I'm parsing have lines with nothing on them, which will cause it to quit early not reading in the whole file. Is there anyway to have it read the number of lines and loop through that way? Or does anyone see another way to do this? My code follows... (just for reference, I'm deleting any line containing a certain string, and the following line)

    Code:
       Function ParseMe(ByVal strFileName As String, ByVal searchString _
                        As String)
    
            Dim objReader As StreamReader
            Dim objWriter As StreamWriter
            Dim noWrite As Integer
            Dim fileDelete As File
            Dim fileCopy As File
    
            ' Open the file
            objReader = New StreamReader(strFileName)
            objWriter = New StreamWriter(strTempFile, False)
    
            ' Declare variable
            Dim strLine As String, strData As String
    
            ' Loop through, compare, write temp...
            Do
                strLine = objReader.ReadLine
    
                If strLine <> Nothing Then
                    If strLine.IndexOf(searchString) > 0 Then
                        objReader.ReadLine()
                        noWrite = 1
                    Else
                        noWrite = 0
                    End If
                End If
    
                If noWrite = 0 Then
                    objWriter.WriteLine(strLine)
                End If
            Loop While strLine <> Nothing
    
            objReader.Close()
            objReader = Nothing
            objWriter.Close()
            objWriter = Nothing
    
            fileCopy.Copy(strTempFile, strFileName, True)
            fileDelete.Delete(strTempFile)
    
    
        End Function

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    I dont think a string can be nothing..check for the string = ""
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2002
    Location
    Oklahoma City, OK
    Posts
    3
    Hmm, well that doesn't seem to be my problem. The problem is that if I have a line with nothing on it, it's returned as nothing thus ending the loop. I don't want it to do that. I want the loop to end at the end of the file, not at the blank line.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    try this

    Code:
    Do Until streamreader.Peek = -1
       ' stuff
    Loop
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2002
    Location
    Oklahoma City, OK
    Posts
    3
    Excellent! Thanks for your help.

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