|
-
Feb 17th, 2004, 11:21 AM
#1
Thread Starter
Addicted Member
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
-
Feb 17th, 2004, 03:58 PM
#2
Lively Member
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:
Dim lines() As String
Dim indx As Integer = 0
While sr.Peek >= -1
redim lines(indx) As String
lines(indx) = sr.ReadLine()
lines(indx) = lines(indx) & ":"
Next
sr.Close()
Dim i As String
For Each i in lines()
sw.Writeline()
Next
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.
-
Feb 17th, 2004, 04:34 PM
#3
Addicted Member
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.
-
Feb 26th, 2004, 06:04 AM
#4
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|