Results 1 to 4 of 4

Thread: Adding string in specific index into already existing text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Adding string in specific index into already existing text file

    What I want to create is a system where the user is filling the registration form once everything is correct and he presses the button, the next form will appear with credit card details and the amount he wants to top up. The username and password is going to be stored in text file in format username|password|money. Then user can log into their account and add more balance.


    By using jmcilhinney suggestion with a list (of strings) () read last and split the last line, and then insert the money. However, when user login to their account they have to top their account but this time, the login|password|money could at the very first line.

    Could anyone would give a hint what would be the best way of implementing this system, I have registration part done I just need to get an idea of how to finish the rest. I would really appreciate any suggestions/ Thank you

    Code:
    Private Sub btnComplete_Click(sender As Object, e As EventArgs) Handles btnComplete.Click
    
            Dim top As Short
            Short.TryParse(cboTop.SelectedItem > -1, top)
    
            Dim list As New List(Of String)()
    
            Dim reader As StreamReader = New StreamReader("..\..\TextFile\users.txt", True)
            Dim line As String
            Dim readAmount As String()
    
            line = reader.ReadLine()
            Do While Not (line IsNot Nothing)
                line = line.Trim
                readAmount = line.Split("|")
    
                For i As Integer = 0 To readAmount.Length - 1
                    If readAmount(i).Length > 0 Then
                        list.Add(readAmount(i).Trim.ToString)
    
                    End If
                Next
    
                line = reader.ReadLine()
    
                Dim writer As StreamWriter = New StreamWriter("..\..\TextFile\users.txt", True)
    
    	    writer.WriteLine
    
    	' Im stock here 
    
            Loop
    
    
        End Sub

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Adding string in specific index into already existing text file

    Do you want to search "users.txt" file for logged in user and update his balance? if so then try this method
    Code:
        Private Sub UpdateUserBalance(strFile As String, strUserName As String, decBalance As Decimal)
            Const delimiter As Char = "|"c
            Dim users() As String = File.ReadAllLines(strFile)
            Dim data() As String
            'Search "users.txt" file for the user name.
            For j = 0 To users.Length - 1
                If users(j).Trim.Length > 0 Then ' skip empty lines.
                    data = users(j).Trim.Split(delimiter)
                    If data(0).ToLower = strUserName.ToLower Then ' found the user name. (remove ToLower if the user name is case sensitive)
                        data(2) = decBalance.ToString ' update his balance.
                        users(j) = Join(data, delimiter)
                        File.WriteAllLines(strFile, users) ' write after updating.
                        Exit For ' no need to complete the loop.
                    End If
                End If
            Next
        End Sub



  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Adding string in specific index into already existing text file

    This is a perfect example of what happens when you try to write code without knowing what that code actually has to do. Twice now you have written code that really doesn't make sense because you have no actual plan to compare the code you write to. Before you write any code, you should pick up a pen and paper and write down, in detail, what the code is actually supposed to do. That plan should be something that you could hand to someone with no prior experience with the problem and that they could follow to get the job done. Once you have that detailed plan, you can start writing code to implement it. At any stage, you can compare your code to your plan to check whether what you've written actually makes sense. If the code doesn't do what it's supposed to then you know that either it doesn't implement the plan at some point or there's something wrong with your plan. If you write code without a plan then you end up in the mess you are now, with no idea where there might be an issue in your code because you have no clear idea of what the code is supposed to do.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Adding string in specific index into already existing text file

    Thank you, I'll try.

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