0

I've seen this question on here a few times but I can't seem to find the right answer. I currently have a Windows Form Application I'm writing in VB that creates player profiles (this is a school project). On the 'Registration' page I take in the information and read it to a .txt file with each field comma deliminator line by line. This makes for a .txt that looks something like this:

 Dan,Boyle,M,39,(707) 555-1234,555 S. West Street,18,D,R
 Justin,Braun,M,27,(916) 555-1255,858 W. South St,5,D,L

I ran right new lines to this, and call this just fine but what I need to do for my project is be able to edit a player profile. What I've found so far is that the best way would be to re-write the whole line, which I've managed to do. But what I can't figure out is how to remove the current line and replace.

    Dim FileString As String = "MasterRoster.txt"
    Dim index As Integer = Home.cbPlayerLookUp.SelectedIndex

    Dim playerInfo() As String = File.ReadAllLines(FileString)
    Dim FieldString() As String

    Dim RecordString As String = ""


    If Not playerInfo.Length < index Then
        FieldString = playerInfo(index).Split(","c)
        'Update Player stats)

        If CInt(FieldString(16)) = index Then

            FieldString(12) = CStr(CInt(FieldString(12)) + CInt(txtGoals.Text))
            FieldString(13) = CStr(CInt(FieldString(13)) + CInt(txtAssists.Text))
            FieldString(14) = CStr(CInt(FieldString(14)) + CInt(txtPIM.Text))
            FieldString(15) = CStr(CInt(FieldString(15)) + CInt(txtGames.Text))

            '(FirstName0, LastName1, Gender2, Age3, Phone4, Address5, Experience6, Position7, Handedness8, Team9, Jersey#10, imgPath11, Goals12, Assists13, PIM14, Total Games15, playerID Number16)
            RecordString = FieldString(0) & "," & FieldString(1) & "," & FieldString(2) & "," & FieldString(3) & "," & FieldString(4) & "," & FieldString(5) &
                "," & FieldString(6) & "," & FieldString(7) & "," & FieldString(8) & "," & FieldString(9) & "," & FieldString(10) & "," & FieldString(11) & "," & FieldString(12) & "," &
                FieldString(13) & "," & FieldString(14) & "," & FieldString(15) & "," & FieldString(16) & vbNewLine



        End If

        My.Computer.FileSystem.WriteAllText(FileString, RecordString, True)

    End If

This will currently add a new line to the bottom of my .txt file "MasterRoster.txt" with the updated information at the end, but the old one is still there. How can I instead replace the old line?

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
j_fulch
  • 125
  • 2
  • 13
  • text CSV files are not random access. you'll have to replace the file skipping the old record and adding the new one. – Ňɏssa Pøngjǣrdenlarp May 01 '14 at 01:09
  • got ya. So...I'd need to read in old file, change what I want to change, and re-print – j_fulch May 01 '14 at 01:15
  • 1
    since you apparently dont have the file loaded into memory, yes: read it line by line from one stream, look for the name just changed and skip it if they match, otherwise write the line back to the out stream, then append the changed record. there **are** more efficient ways since you had to read it in to get the record to edit. also, there are much MUCH better storage mechanisms than old fashioned arrays, but that might be part of the point the assignment. – Ňɏssa Pøngjǣrdenlarp May 01 '14 at 01:19
  • Exactly, I'd much rather use a database, but the assignment is requiring us to use a .txt – j_fulch May 01 '14 at 01:29
  • if `playerInfo` was a module level variable, youd only have to read the file once (ever). for the change/edit you could replace the fields then write it all out once when they hit Save – Ňɏssa Pøngjǣrdenlarp May 01 '14 at 01:34

1 Answers1

0

Since you are reading the whole file into memory, using a class to represent the info allows you to put meaningful names to each field.(Will you remember what each field represents a year or two from now?) One way to hold a collection of that data is in a DataTable which is very easy to use with a DataGridView, to display the data for reading and editing. Another option is to use an .xml text format instead of a .csv text format. This allows easier reading and writing to and from the file, since DataTable has methods designed for that.

In any case editing specific records will require over-writing or re-writing the file, unless, of course you want to take a step backwards and use the old legacy methods, FileOpen, FileGet, and FilePut.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • Absolutely, like I did mention. This is for a class, the professor requests that we use a .txt file. These are great suggestions though. – j_fulch May 01 '14 at 17:51
  • an xml is basically a text file. If necessary just name it with a .txt extension. The datatable will still read it. – tinstaafl May 01 '14 at 19:22