Results 1 to 4 of 4

Thread: file io processing

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129

    Question file io processing

    Hi All,

    can anyone tell me where I have gone wrong ?? I am trying to read in a text file and add a ":" to the end of each line, this is what I have so far:

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
    Dim path As String = "c:\mydata\test.txt"

    Try
    'If File.Exists(path) Then
    'File.Delete(path)
    'End If

    Dim sw As StreamWriter = New StreamWriter(path)
    Dim sr As StreamReader = New StreamReader(path)

    If sr.Peek() >= -1 Then
    sw.WriteLine(":")
    End If
    sw.Close()
    sr.Close()

    Catch e As Exception
    Console.WriteLine("The process failed: {0}", e.ToString())
    End Try

    End Sub

    Also, if anyone knows how to remove blank lines, this would also be appreciated.

    dagoose

  2. #2
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    dagoose, there are a lot of text file processing threads already on here, just do a search.

    But since I'm here...

    This is totally off the cuff and untried, but under Try you might want to do something like:
    VB Code:
    1. Dim lines() As String
    2. Dim indx As Integer = 0
    3.  
    4. While sr.Peek >= -1
    5.      redim lines(indx) As String
    6.      lines(indx) = sr.ReadLine()
    7.      lines(indx) = lines(indx) & ":"
    8. Next
    9. sr.Close()
    10.  
    11. Dim i As String
    12. For Each i in lines()
    13.      sw.Writeline()
    14. Next
    15. sw.Close()

    I don't know for sure if you can have StreamReader and StreamWriter working on the same file at the same time. Either way, you will need a loop to read and write each line of the text file.

  3. #3
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195
    This should do what you want:


    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim path As String = "c:\test.txt"
    
            'Count the lines in the file
            Dim LineCounter As New StreamReader(path)
            Dim linecount As Integer = 0
            Do Until LineCounter.Peek = -1
                LineCounter.ReadLine()
                linecount += 1
            Loop
            LineCounter.Close()
    
            'An array to hold the lines
            Dim Line(0, linecount - 1) As String
    
            'Read the lines and enter them into the array
            Dim LineReader As New StreamReader(path)
            For FileLine As Integer = 0 To linecount - 1
                Line(0, fileline) = LineReader.ReadLine
            Next FileLine
            LineReader.Close()
    
    
            'Pull the line from the array and write them to file
            Dim LineWriter As New StreamWriter(path)
            For FileLine As Integer = 0 To linecount - 1
                LineWriter.WriteLine(Line(0, fileline) & ":")
            Next
            LineWriter.Close()
    
        End Sub



    Change the last section to this to remove blank lines in that file:

    Code:
    'Pull the line from the array and write them to file
            Dim LineWriter As New StreamWriter(path)
            For FileLine As Integer = 0 To linecount - 1
                If Not Line(0, fileline) = "" Then
                    LineWriter.WriteLine(Line(0, fileline) & ":")
                End If
            Next
            LineWriter.Close()
    Last edited by Hole-In-One; Feb 17th, 2004 at 04:42 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129
    many thanks for your help,

    dagoose

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