beginner help working with text files

G

Gaz

Hi all
I am trying to write a program that opens a text file and manipulates the data.

I have managed to open the text file into a textbox1 as a string
i now wish to add code to a button that will find a line in the
string that begins with a certain letter eg I and when it does i want to
insert a value eg 2 into a set character distance on the line. Can
anyone help me with a loop that could do this or another way please?
example code would be greatly appreciated thanks

Kind Regards,

Gary


Just to add im using visualbasic.net standard edition and i want to use
the code in a program not on a webpage... thanks in advance
 
M

Micky

Gaz said:
string that begins with a certain letter eg I and when it does i want to insert a value eg 2 into a set character
distance on the line. Can anyone help me with a loop that could do this or another way please?


Just to add im using visualbasic.net standard edition and i want to use the code in a program not on a webpage...
thanks in advance

Instead of treating the text file as a single string of text, treat it as a series
of lines (each line being a separate string).

For example:

Imports System.IO

Sub LoadFile(ByVal Filename As String)
Try
Dim fStream As New FileStream(Filename, FileMode.Open)
Dim sReader As New StreamReader(fStream)
Dim sRead As String = Nothing
With TextBox1
.Text = Nothing
While Not sReader.Peek = (-1)
sRead = sReader.ReadLine
'
' process the sRead string here
'
.Text &= sRead
sRead = Nothing
End While
End With
Catch Ex As Exception
MsgBox(Ex.Message, MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "LoadFile Error")
Finally
sReader.Close
fStream.Close
End Try
End Sub
 
G

Gaz

Micky said:
Instead of treating the text file as a single string of text, treat it as a series
of lines (each line being a separate string).

For example:

Imports System.IO

Sub LoadFile(ByVal Filename As String)
Try
Dim fStream As New FileStream(Filename, FileMode.Open)
Dim sReader As New StreamReader(fStream)
Dim sRead As String = Nothing
With TextBox1
.Text = Nothing
While Not sReader.Peek = (-1)
sRead = sReader.ReadLine
'
' process the sRead string here
'
.Text &= sRead
sRead = Nothing
End While
End With
Catch Ex As Exception
MsgBox(Ex.Message, MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "LoadFile Error")
Finally
sReader.Close
fStream.Close
End Try
End Sub
thanks ill give it a try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top