|
-
Dec 3rd, 2009, 10:14 AM
#1
Thread Starter
Lively Member
[RESOLVED] Extra newline chars on file append
Hello. I am writing a program that will read and write to a file using the My.Computer.Filesystem namespace, and when I append my data file with the code shown below under SaveFile subroutine, it sometimes places extra ControlChars.Newline at the end of the appended text. Sometimes it places one extra newline, and sometimes it adds 3-6 newlines. I have tried everything I can think of to make it add just one newline at the end of each append, as I will be using the newline char to flag each entry on retrieval. I am working out some algorithms in this project to use in a project that will keep track of data in a file using csv format(just practicing saving/retrieving data from a file). The extra newline characters that are added will become a problem when I start to sort the data to display. Any help will be appreciated.
my xSaveButton click event:
Code:
Private Sub xSaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xSaveButton.Click
Dim boolAppendData As Boolean
If My.Computer.FileSystem.FileExists("myData.dat") = True Then
Dim overWrite As String = MsgBox("Overwrite data file? Selecting yes will overwrite" & vbCrLf & _
"the data file. Selecting no will append to it.", MsgBoxStyle.YesNo, "Data File")
If overWrite = DialogResult.Yes Then
boolAppendData = False
Else
boolAppendData = True
End If
SaveFile(boolAppendData)
Else
SaveFile(False)
End If
End Sub
my save subroutine:
Code:
Private Sub SaveFile(ByVal boolAppend As Boolean)
If Me.xTextBox.Text = String.Empty Then
MsgBox("There is nothing to save to the file." & vbCrLf & "Type something in the textbox " _
& vbCrLf & "or open a file first.", MsgBoxStyle.OkOnly, "Empty Textbox")
Else
Try
My.Computer.FileSystem.WriteAllText("myData.dat", Me.xTextBox.Text & ControlChars.NewLine, boolAppend)
MsgBox("File saved successfully.", MsgBoxStyle.OkOnly, "File Saved")
Catch ex As Exception
MsgBox("There was a problem saving the file." & vbCrLf & ex.ToString, MsgBoxStyle.OkOnly, "File Not Saved")
End Try
End If
End Sub
Last edited by Brian M.; Dec 3rd, 2009 at 10:17 AM.
-
Dec 3rd, 2009, 10:53 AM
#2
Re: Extra newline chars on file append
Can you confirm that the newline characters are not already there before you append your text?
My usual boring signature: Nothing
 
-
Dec 3rd, 2009, 02:51 PM
#3
Thread Starter
Lively Member
Re: Extra newline chars on file append
Yes. I have went so far as to format the text file by hand first. And when appending to the file, I have made sure the cursor was on the same line as the last line of text to be appended before I click the save button. And it doesn't happen all the time either. It acts like it keeps a count and increments the newline character by one each time I append it. So first time it works fine, the next time there is one extra newline, then two, ect.
-
Dec 3rd, 2009, 03:05 PM
#4
Addicted Member
Re: Extra newline chars on file append
1] Dont use 'ControlChars.NewLine' use 'Environment.NewLine' it is less buggy and more secure as i figured back when i had a problem like this.
2] Code:
Code:
Public Function RemoveNewlinesAtEnd(Dim Text As String) As Single
If Text.EndsWith(Environment.Newline) Then
Text = Text.Substring(0, Text.Length - Environment.Newline)
End If
End Function
TextBox1.Text = RemoveNewlinesAtEnd(TextBox1.Text)
I have not tested it but if you get an error change the:
Code:
Text = Text.Substring(0, Text.Length - Environment.Newline)
to
Code:
Text = Text.Substring(0, Text.Length - 1)
Last edited by dcrew; Dec 3rd, 2009 at 03:11 PM.
-
Dec 3rd, 2009, 03:34 PM
#5
Thread Starter
Lively Member
Re: Extra newline chars on file append
Your implementation is pretty much what I had in mind for reading in the values from the file. Except I will be using indexOf to get the newline position, then subtracting it from a variable that stores the first position of the character right after the preceding newline, thereby placing each 'line' into a string array. I replaced the controlchars.newline with the environment.newline, and it seems to have stopped adding the random newlines to my appends. Thanks for that.
-
Dec 3rd, 2009, 03:36 PM
#6
Hyperactive Member
Re: [RESOLVED] Extra newline chars on file append
nevermind, it has been resolved
Last edited by Philly0494; Dec 3rd, 2009 at 03:40 PM.
-
Dec 3rd, 2009, 03:45 PM
#7
Thread Starter
Lively Member
Re: [RESOLVED] Extra newline chars on file append
Tanks anyways philly. But if you have another suggestion, I will be glad to hear it. There is always more than one way to skin a cat.
-
Dec 3rd, 2009, 03:55 PM
#8
Fanatic Member
Re: [RESOLVED] Extra newline chars on file append
I recently ran into a similar problem. What I noticed is that if I processed the string so that it was just digits with the return characters and I tested the string with IsNumeric it said it was numeric even though return characters were in there. I wanted a number in the final string so after processing the string to get the digits and the return character I converted to decimal :
Code:
adec = CDec(st1) ' st1 is the string and adec is declared as decimal
and then put adec back in the string and that way got rid of the return characters. When I had processed the string for instance if my result was 10 and I asked for the length of the string it said 4 so it had two return characters in there. I tried
Code:
If st1.IndexOf(Chr(13)) <> -1 Then
st1.Replace(Chr(13), "")
End If
If st1.IndexOf(Chr(10)) <> -1 Then
st1.Replace(Chr(10), "")
End If
but it didn't strip out the return. What I found just now is that I could remove the return from the string by using remove. I got the length of the string and removed the 2 characters whatever they were and that way is another way I could get rid of the return. So if you properly process the string you can remove the line return using remove.
Here's some code that worked in my situation. Maybe you could use IsLetterOrDigit instead of IsNumeric.
Code:
st1L = st1.Length
a1 = 0
If st1L > 0 Then
For i = 0 To st1L - 1
If IsNumeric(st1.Substring(i, 1)) = False Then
a1 += 1
End If
Next
If a1 > 0 Then
st1 = st1.Remove(st1L - a1, a1)
End If
End If
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Dec 3rd, 2009, 04:01 PM
#9
Thread Starter
Lively Member
Re: [RESOLVED] Extra newline chars on file append
Well maybe in your code where you look for chr(13), you could instead look for controlchars.newline or environment.newline instead of converting 13 to its ascii value. Maybe that will be a bit cleaner. But thanks for the suggestion. Need a string.trimNewline
function, hehe.
-
Dec 3rd, 2009, 04:22 PM
#10
Addicted Member
Re: [RESOLVED] Extra newline chars on file append
Good Working and Helping also Resloving your problem for you! Anymore questions or code you need then PM me or post a new topic.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|