Results 1 to 10 of 10

Thread: [RESOLVED] Extra newline chars on file append

Threaded View

  1. #1

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Resolved [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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width