Reputation: 11
Hey so I'm trying to do a simple StreamWriter for adding staff information to a text file (I already have a working StreamReader log in page) , the text file has some sample data but also must be able to have new data written to it.
My problem is that ONLY upon initial start up and my initial writing of data the information is written on the same line as the last line of sample data in the text file, not on the next blank line. Here is how the text file looks after the first attempt to write data after start up.
"Me,Too" being my last line of sample data and "Grey,Hardy" being my first data written via StreamWriter
I've done some research on here and found examples of how to write a blank line which you could make the StreamWriter do every time to put spacing in between etc. but that doesn't work here as this problem only occurs the first time on any given start up and if it left a blank line every single time then if I made two attempts to use the StreamWriter it would look like
Which would then enable a blank log in to work.
Here is my code for the StreamWriter
'StreamReader to check if a line is empty or not
Dim StaffReader As StreamReader = File.OpenText("StaffInfo.txt")
strLine = StaffReader.ReadLine()
StaffReader.Close()
'Streamwriter for adding staff log in to textfile
Dim FileCheck As StreamWriter = File.AppendText("StaffInfo.txt")
FileCheck.WriteLine(strStaffUsername2 & "," & strStaffPass2)
FileCheck.Close(``)
MessageBox.Show(strStaffUsername2 & " added to the file. ", "Data Added")
txtUser2.Clear()
txtUser2.Focus()
txtPass2.Clear()
I just want it to skip to the next blank line if the line it is going to write on has any characters on it.
Upvotes: 1
Views: 1003
Reputation: 5449
what you are looking for is the point, if the last line in your file contains a carriage return or not. If it does contain a carriage return, then you can append your text with your written code. But if it does not contain a carriage return, you have to prefix your first line with a carriage return.
Thus, first you have to check if your last line has a carriage return. You can do that by a function like this:
Private Function DoesLastLineHasCarriagReturn(Filename As String) As Boolean
Dim s as String = ""
using stream = New StreamReader(Filename)
' we have to read the last two characters
stream.BaseStream.Seek(-2,SeekOrigin.End)
dim c_beforeLast as Integer = stream.Read()
Dim c_Last As Integer = stream.Read()
' if the last character is 10 -> we have a carriage return. Windows and Linux just use different encodings
if (c_Last =10)
If(c_beforeLast = 13)
Console.WriteLine("Ends with carriage return CRLF (Windows)")
Else
Console.WriteLine("Ends with carriage return LF (UNIX, OSX )")
End If
return true
Else
Console.WriteLine("Does not end with Carriage return")
End If
End Using
return false
End Function
Then when beginning to write in the file, first you have to call the function. Here how your code could look like:
If DoesLastLineHasCarriagReturn("StaffInfo.txt")
Dim FileCheck As StreamWriter = File.AppendText("StaffInfo.txt")
FileCheck.WriteLine("Greg" & "," & "Hardy")
FileCheck.Close()
Else
Dim FileCheck As StreamWriter = File.AppendText("StaffInfo.txt")
FileCheck.WriteLine(vbCrLf + "Greg" & "," & "Hardy")
FileCheck.Close()
End If
By the way, I would recommend you to use the using
statement to close the stream automatically:
If DoesLastLineHasCarriagReturn("StaffInfo.txt")
Using writer As New StreamWriter("StaffInfo.txt", True)
writer.WriteLine("Greg" & "," & "Hardy")
End Using
Else
Using writer As New StreamWriter("StaffInfo.txt", True)
writer.WriteLine(vbCrLf +"Greg" & "," & "Hardy")
End Using
End If
Upvotes: 1
Reputation: 15091
Since we are dealing with a text file and not doing anything fancy, we can just use the File class in System.IO.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FilePath = "TestFiles/File1.txt"
'Dump contents of file
Dim fileContents As String = File.ReadAllText(FilePath)
'Extract the last 2 characters in the contents
Dim NextToLast As String = fileContents(fileContents.Length - 2)
Dim LastCharacter As String = fileContents(fileContents.Length - 1)
'Check if we have a CrLf an if not add a new line
If Not NextToLast = Chr(13) AndAlso Not LastCharacter = Chr(10) Then
File.WriteAllText(FilePath, Environment.NewLine)
End If
'Write to the file
File.AppendAllLines(FilePath, {TextBox1.Text})
End Sub
To display your text file, add a ListBox to your form.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Put the contents of the file into a string array of lines
Dim staff() As String = File.ReadAllLines("TestFiles/File1.txt")
ListBox1.DataSource = staff
End Sub
Upvotes: 0