locked
Remove new line at end of a csv file after a streamwriter write RRS feed

  • Question

  • I have a csv file that contains data that is added programmatically through a stream writer.

    After the entry is added by the 'writeline' method, the file contains an extra blank line at the EOF.

    How do I eliminate it while adding a new 'writeline' item.

    Here is my sub code:

     Private Sub btnAddMold_Click(sender As Object, e As EventArgs) Handles btnAddMold.Click
    
            Dim info As String = ""
    
            info = String.Format("{0},{1},{2},{3}", "Bench Leg- Omega", "H-16", "1.75", "52")
    
            Dim objWriter As New System.IO.StreamWriter(StartForm.GlobalVariables.moldFile, IO.FileMode.Append)
    
    
            objWriter.WriteLine((vbCrLf & info).TrimEnd)
    
            objWriter.Close()
    
            MessageBox.Show("Text Appended to the File")
    
        End Sub

    The resulting appended entry gives me the newline of data at the EOF, but also does a CrLf for the next line 'after' the new entry.

    I need to eliminate that white space created on the very last line in the file for my streamreader to correctly read and add to a dgv.

    Thanks in advance.

     



    • Edited by Pete - Monday, June 11, 2018 7:49 PM
    Monday, June 11, 2018 7:45 PM

Answers

  • Hi

    This works OK without spurious VbCrLf. You were introducing your own VbCrLf.

        Dim info As String = String.Format("{0},{1},{2},{3}", "Bench Leg- Omega", "H-16", "1.75", "52")
    
        Using objWriter As New IO.StreamWriter(StartForm.GlobalVariables.moldFile, True)
    
          objWriter.WriteLine(info.TrimEnd)
    
        End Using
        MessageBox.Show("Text Appended to the File")


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, June 11, 2018 8:06 PM
    • Marked as answer by Pete - Monday, June 11, 2018 8:57 PM
    Monday, June 11, 2018 8:05 PM

All replies

  • Hi

    This works OK without spurious VbCrLf. You were introducing your own VbCrLf.

        Dim info As String = String.Format("{0},{1},{2},{3}", "Bench Leg- Omega", "H-16", "1.75", "52")
    
        Using objWriter As New IO.StreamWriter(StartForm.GlobalVariables.moldFile, True)
    
          objWriter.WriteLine(info.TrimEnd)
    
        End Using
        MessageBox.Show("Text Appended to the File")


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, June 11, 2018 8:06 PM
    • Marked as answer by Pete - Monday, June 11, 2018 8:57 PM
    Monday, June 11, 2018 8:05 PM
  • Thanks for responding.

    I get this result:

    It is added directly after the last line of data.

    And the yellow mark in the lower left still has a flashing cursor, which means space has been added to the file at the very end- I need that removed also.

    That is why I had the 'VbCrLf' before the 'info' string to get to the next line.

    Thanks for your input- any other ideas- I have tried many remedies- none have worked.

    Monday, June 11, 2018 8:15 PM
  • Hi

    Using the code I posted (with my own path of course), I get nice and clean lines like this (with nothing after the last character). These lines were produced by running the code manually that many times - so the file was close each time and re-opened each time.

    Please post the code again that your last post was using,

    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52
    Bench Leg- Omega,H-16,1.75,52


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, June 11, 2018 8:26 PM
    Monday, June 11, 2018 8:24 PM
  • Not sure why you would use a StreamReader to populate a DataGridView when you have a column delimiter in your file. Have you tried using a TextFiledParser and DataTable instead?

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/cc7d1f3f-ef0e-4b37-92f6-d4990269a166/datagrid-read-csv?forum=vbgeneral


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, June 11, 2018 8:36 PM
  • Hmmmm...

    Well here is what I had originally:

    Private Sub btnAddMold_Click(sender As Object, e As EventArgs) Handles btnAddMold.Click
    
            Dim info As String = ""
    
            info = String.Format("{0},{1},{2},{3}", "Bench Leg- Omega", "H-16", "1.75", "52")
    
            Dim objWriter As New System.IO.StreamWriter(StartForm.GlobalVariables.moldFile, IO.FileMode.Append)
    
    
            objWriter.WriteLine((vbCrLf & info).TrimEnd)
    
            objWriter.Close()
    
            MessageBox.Show("Text Appended to the File")
    End Sub

    Monday, June 11, 2018 8:45 PM
  • Nope, do not want to use a DataTable, keeping the code simple this round.

    It's just a small program for my own business use.

    Monday, June 11, 2018 8:47 PM
  • Hmmmm...

    Well here is what I had originally:

    Private Sub btnAddMold_Click(sender As Object, e As EventArgs) Handles btnAddMold.Click
    
            Dim info As String = ""
    
            info = String.Format("{0},{1},{2},{3}", "Bench Leg- Omega", "H-16", "1.75", "52")
    
            Dim objWriter As New System.IO.StreamWriter(StartForm.GlobalVariables.moldFile, IO.FileMode.Append)
    
    
            objWriter.WriteLine((vbCrLf & info).TrimEnd)
    
            objWriter.Close()
    
            MessageBox.Show("Text Appended to the File")
    End Sub

    Hi

    Hmmm........ I meant the code you used to test my offering - what I'm trying to ascertain is what could give the different results comparing mine to yours.


    Regards Les, Livingston, Scotland

    Monday, June 11, 2018 8:49 PM
  • I just want to add concrete molds as I purchase them into a simple CSV file, I probably will never exceed 200.

    Just wanted to be able to add them into the csv file through the program on the fly.

    I could manually enter them into the csv, but you know, that's what programs are for.


    • Edited by Pete - Monday, June 11, 2018 8:51 PM
    Monday, June 11, 2018 8:50 PM
  • Nope, do not want to use a DataTable, keeping the code simple this round.

    It's just a small program for my own business use.

    I wasn't aware that populating an unbound DataGridView was simple. ;-)

    I'm not exactly sure what is tripping you up. If you are seeing a blank line at the end you should be able to check for that and ignore it, and possibly assume you have reached the end of the file.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, June 11, 2018 8:54 PM
  • If I add a CR into my csv file and then run your code, it works perfectly.

    So, I believe the problem is solved.

    Thank you for your time, I appreciate it.

    It sometimes takes a second or third head to look at it and then see the problems that exist.

    Been programming too long, should have been simple.

    Thanks.

    Monday, June 11, 2018 8:56 PM
  • Yes, your code worked after I manually put a CR into the CSV file to accept new data.

    Thank you very much.

    Monday, June 11, 2018 8:57 PM
  • Yes, your code worked after I manually put a CR into the CSV file to accept new data.

    Thank you very much.

    Hi

    OK,I guess you were using an already populated file. Glad you got it working.


    Regards Les, Livingston, Scotland

    Monday, June 11, 2018 8:58 PM
  • Works, peachy.

    Monday, June 11, 2018 8:59 PM
  • It's very simple.

    What was wrong, was it was starting at the last character of the last entry on the first save, after that it was fine.

    • Edited by Pete - Monday, June 11, 2018 9:01 PM
    Monday, June 11, 2018 9:00 PM
  • I ran into this problem a couple years ago, and forgot how I handled it, and at that time I was using DataTables. Over 35000 lines of code in that commercial program.

    Just needed a refresher for such a simple thing- you know thinking too deep.

    Thanks Paul

    Monday, June 11, 2018 9:03 PM
  • Thanks Les, greatly appreciate it.

    Guess I forgot to tell you I already created the file manually to start with, sorry about that.

    Thank you

    Pete

    Monday, June 11, 2018 9:05 PM