Adding a blank lines into a sequential file

cpopham

Well-known member
Joined
Feb 18, 2004
Messages
273
I have a sequential file and I have to manually isert a blank line every third line down the page. How can I read the file and then automatically insert a blank line (CR LF) after every third line?

Chester
 
Something like the following should do.
Code:
Dim i As Integer

        Dim inFile As New StreamReader("<original file here>")
        Dim outFile As New StreamWriter("<new file here>")

        Dim line As String

        Do
            line = inFile.ReadLine()
            i += 1
            outFile.WriteLine(line)
            If i Mod 3 = 0 Then
                outFile.WriteLine()
            End If
        Loop Until line = ""
        inFile.Close()
        outFile.Close()

if you want to replace the original file simply delete it and rename the new file after the above routine finishes.
 
Okay this is what I have so far, and I cannot get it to work:

Private Sub FirstCharButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FirstCharButton.Click
Dim oDialog As New OpenFileDialog
Dim sReader As IO.StreamReader /// to open the file for reading
Dim sWriter As IO.StreamWriter /// to write to the file

Dim strText, strPath As String


Dim intCount As Integer

With oDialog
.InitialDirectory = "C:\"
.Filter = "textfiles| *.txt"
If .ShowDialog = DialogResult.OK Then
sReader = New IO.StreamReader(New IO.FileStream(.FileName, IO.FileMode.Open))
While Not sReader.Peek = -1
strText += sReader.ReadLine.Remove(0, 1) & Environment.NewLine /// append each line of the fiels text Minus the firt character to a string buffer
End While
sReader.Close() ///Close the opened file

sWriter = New IO.StreamWriter(.FileName, False) /// False will overwrite the existing file
sWriter.Write(strText)
sWriter.Close()

Do
strText = sReader.ReadLine()
intCount += 1
sWriter.WriteLine(strText)
If intCount Mod 3 = 0 Then
sWriter.WriteLine()
End If
Loop Until strText = ""

End If
End With

End Sub

Also, Is there a way for me to get the count of the first character of the first line and say store it in a intNum Variable, before the code writes the lines back into the file?

Thanks, Chester
 
You have already closed the StreamReader and the stream writer at that point where you pasted the code.
So, instead of having the While loop and all of that stuff in it, instead use the Do Loop that you have there... and then use strText.Remove(0, 1) after you have ReadLine.
To replace the file, delete the old one and rename the new one to the same name as the old one.

What do you mean the count of the first character? You mean the current line number? The line number is stored in intCount (starting at zero).
If you are trying to read a character from the string, use .Read in your StreamReader to read one character. :)
 
Okay I will give that a try. I am talking about string manipulation for example intIndex = strText.IndexOf(" ", 1)? Can I do something like that while the string has been read from the file.
 
Yes, you can do that to find the first space in the file.
To get the first character of a string, use .SubString(0, 1) (starting at zero, read one character). :)
 
Mod is modulus math. It is division and returns the remainder of the division.

Example: 3 mod 3 = 0 102 mod 20 = 2 because 2 would be the remainder.

if the counter mod 3 = 0 then you are on the third line.

Chester
 
Back
Top