Inserting a gap in each line of a text file.

Joined
Nov 9, 2009
Messages
16
Programming Experience
Beginner
Greetings

I have a text file that I need to go to column 40 and insert 10 spaces. What appears to happen is it takes the filst line and appends 10 spaces for every line that occurs.

Here is my code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

Dim openFileDialog1 As New OpenFileDialog()
Dim TextLine As String = " "
Dim newTextLine As String = " "
Dim strGap As String = " "

openFileDialog1.InitialDirectory = "C:\Debug\"
OpenFileDialog1.Title = "GEOPAK Report Modifier"
OpenFileDialog1.Filter = "Clearing Limits(*.clr)|*.clr|Seeding(*.ser)|*.ser|Slope Stakes(*.ssb)|*.ser|Text Files(*.txt)|*.txt|XYZ Files(*.xtb)|*.xtb|All Files(*.*)|*.*"


If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim objReader As New System.IO.StreamReader(openFileDialog1.FileName)
Do While objReader.Peek <> -1
TextLine = TextLine & objReader.ReadLine() & vbNewLine
If TextLine.Length > 10 Then
TextLine = TextLine.Insert(40, strGap)
End If
Loop
RichTextBoxPrintCtrl1.Font = New Font("Lucida Console", 10, FontStyle.Regular)
RichTextBoxPrintCtrl1.Text = TextLine
End If
End Sub

So when its done it would look like this:


TTTTTTTTT TTTTTTTTTTTTTTT
rather than
TTTTTTTTTTTTTTTTTTTT

I've attached the text file I'm working with. Any help would be very much appreciated.
 
So are you saying you need it to add the spaces to just the first line or all the lines? And no attached file.
 
I understand flow in your example however I run into one of the issues I had before. The first three lines in the attached file don't have 40 characters in the lines. So when the program gets to the insert line it fails. That's why I had the syntax

If TextLine.Length > 10 Then
TextLine = TextLine.Insert(40, strGap)
End If

That's the only way I could think of to get around that issue. Once I did that then I had the issue of getting the gap in only the lines that had more than 10 charaters.
 
Sorry I messed up my logic...Here it is again...
VB.NET:
Dim list As New List(Of String)
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Using sr As New StreamReader("C:\test.txt")
      While Not sr.EndOfStream
        list.Add(sr.ReadLine)
      End While
    End Using
    Using sw As New StreamWriter("C:\test.txt")
      For Each s As String In list
        If Not s = "" And s.Length > 40 Then
          sw.WriteLine(s.Insert(40, "  "))
        Else
          sw.WriteLine(s)
        End If
      Next
    End Using
  End Sub
 
I see what it's trying to do but I've got to be doing something wrong. It wipes everything out of the source file and never writes anything back. You have given me an idea I hadn't thought of. That's to use a list box rather than a RichtextBox. I'll keep working on this with a listbox. Thank you for the help. If think of anything else I welcome the input.
 
It should write it back, mine does. We are getting the list from the List(Of String) collection or you could use the listbox for viewing, and just modifying the values if they match our requirements and writing them to the file.
 
I just did a direct copy and paste from your code and put it in a project with just a button. I then put a file dirctly on the root called test.txt The only thing I changed was the name of the file to write it as. I called it Test1.txt and it worked. So no I need to go through my code and see where I went buggy. Thank you for the help and the patience.
 

Latest posts

Back
Top