Question Csv File not updating

maurice1811

New member
Joined
May 5, 2015
Messages
2
Programming Experience
Beginner
Here is the login process, record adding and updating, I cant see where I'm going wrong,
*******The User will login to their account *****
sub login()


        FileOpen(1, CurDir() + "\LoginDetails.csv", OpenMode.Input) 
         logposition1  = 0
        Do Until EOF(1)
            fullline1 = LineInput(1)
            Dim item() As String = Split(fullline1, ",") 'item () is the array that is splt
            athlete(logposition1).username = item(0) 'load the first item in the line
            athlete(logposition1).cpassword = item(1) ' load the 2nd item in the line
            athlete(logposition1).id = item(6)
            logposition1 += 1 'counter increase by 1 each time to load a new line each time
        Loop


For count1 As Integer = 0 To logposition1
                If username = athlete(count1).username And password = athlete(count1).cpassword Then
                    IDnum = athlete(count1).id 
                    addrun()
endsub()



*****And then add a training record************
sub addrun()

dim x as integer
console.writeline("choose workout")
console.writeline(1- Run 1 mile")
x = console.readline
if x = 1 then
cal = 10

athlete(IDnum).totalcal += cal

update()
end sub

********here's the update process ***********
sub update()


        FileOpen(1, CurDir() + "\LoginDetails.csv", OpenMode.Input)
       logposition = 0
 Do Until EOF(1)
            fullline1 = LineInput(1)
            Dim item() As String = Split(fullline1, ",") 'item () is the array that is splt
            athlete(logposition1).totalcal = item(7)
            logposition1 += 1 'counter increase by 1 each time to load a new line each time
        Loop
       
        FileOpen(1, CurDir() + "\LoginDetails.csv", OpenMode.Output) ' this allows data to be added to the file
        For count As Integer = 0 To logposition1 ' count is any number between 0 and i
            fullline = athlete(count).username & "," & athlete(count).cpassword & "," & athlete(count).id & "," & athlete(count).totalcal
            PrintLine(1, fullline)


        Next

endsub()



**it seems to not be working correctly as all other lines in the file are deleted apart from the one that need to be updated**
ANY HELP WOULD BE APPRECIATED
 
Last edited by a moderator:
Please provide a FULL and CLEAR description of what you expect to happen and what actually happens. Don't expect us to work out what's supposed to happen from code that doesn't actually do it.
 
My bad, there's four items on each line in the file e.g.
Name Username Id Totalcal
item(0). Item(1). Item(2). Item(3)

Then the' athlete(idnum).totalcal += 10' is adding 10 to item(3) on the line IDnum

i want to only update that that specific item in the file with the new value without affecting the rest of the items

the file will look like this

Username0. Password0. Id0. Totalcal0
Username1. Password1. Id1. Totalcal1
Username2. Password2. Id2. Totalcal2


say if I want to updat totcal 1 all other lines will be deleted apart from line 1
 
Last edited:
You can't really update a specific part of a text file. Basically, you need to read the whole file in, edit the part you want to modify, then write the whole lot out again.
 
Use StringBuilder, Read original file and go through all the lines; update the record with matching ID. Now, close the original file and delete it; write the new data which you've assembled in StringBuilder to new file with same file name. At this moment, this is the only solution I see for your problem; I will still check it out if you can read and write to the same file without using StringBuilder.
 
Back
Top