Basically I use text files to hold the program data, which is loaded in to variables when the program starts.

I use StreamWriter to write to the text files when they update within the program. This is fine although the writer adds a extra line when I do not want this, which causes the program to error on the next use.

This is what I mean:

For x = 1 to 4
FileWriter.WriteLine(user(x) & "," & pass(x))
Next

Comes out as (the text doesn't matter here)

'line1
'line2
'line3
'line4
| <---- but the cursor goes here making the application think that there is a extra line of data which there isn't hence causing it to error.

I also use the 'how many lines in the file' as a variable in various parts of my program.

Please could someone shed any light on this issue.
Many Thanks :)

You could adjust your loop so the fourth iteration uses write instead of writeline or whatever n is if it isn't always 4). Or add proper error checking code to your code that reads the files so it can recover from encountering an empty line (the fact that it crashes means you haven't coded for all eventualities).

Hi!

You can append "NewLine" character at the end of each line as demonstrated in the sample, and use it with Write() and TrimEnd() method, Run this code in a separate project you will see a file at location "C:\abc.txt"

Sub Main()

        Dim user() As String = {"item1", "item2", "item3", "item4", "item5"}
        Dim sw As New IO.StreamWriter("C:\abc.txt")
        Dim pass() As String = {"line1", "line2", "line3", "line4", "line5"}
        Dim str As String = String.Empty

        For x As Integer = 1 To 4
            str &= user(x) & "," & pass(x) & Environment.NewLine ' Append new line where required
        Next

        sw.Write(str.TrimEnd(vbCrLf.ToCharArray()))   ' Remove newline at the end
        sw.Close()

        'output
        'item2, line2
        'item3, line3
        'item4, line4
        'item5, line5
    End Sub

This works fine now. Sorry for the late reply.

Many thanks for that :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.