Reading a Text file?

Sinz

Member
Joined
Feb 14, 2008
Messages
6
Programming Experience
Beginner
hey guys and gals,

i have managed to write the username and password to a text file (registration process)

now when the user logs in i need to check the username and password in the text file to what the user has put in. this is where my problem starts i have no idea what i should be doing for this: this is the code i have so far but any help would be very much appreciated:
VB.NET:
      Dim password, Username, contents As String
        Dim i As IO.StreamReader = IO.File.OpenText("Register_Records.txt")

        password = txtPassword.Text
        Username = TxtUsername.Text


        contents = i.Read()
        contents.Split("|")
        MsgBox(contents)
        If contents = Username & password Then
            Edit_Competitor.Show()
        Else
            MsgBox("incorrect Username/password.")
        End If
i was hoping that this would just give me the user name and password from the text file in the message box, as i separated them with "|". i also have no idea how to scan or check the file for the username and password this is just a rough guess as to what i would have to do. please help me i need to hand this assignment in in 14hours!!! hehe...

many thanks, Sinz
 
When you split the contents on the "|", you never assign it to an array:
VB.NET:
Dim SplitContents() As String = contents.Split("|")
Messagebox.Show(SplitContents(0) & Environment.NewLine & SplitContents(1))

If SplitContents(0) = TxtUsername.Text AndAlso SplitContents(1) = TxtPassword.Text Then
  'Passed
Else
  'Failed
End If
 
VB.NET:
     MessageBox.Show(SplitContents(0) & Environment.NewLine & SplitContents(1))

came up with an error:

Index was outside the bounds of the array.

but this is really great, thank you very much for your help especially at this time of night.
 
That means there isn't two fields split by a "|", which is the opposite of your original post

Could you post an snippet of the text file?
 
VB.NET:
  Dim password, Username, contents As String
        Dim i As IO.StreamReader = IO.File.OpenText("Register_Records.txt")


        password = txtPassword.Text
        Username = TxtUsername.Text


        contents = i.Read()


        Dim SplitContents() As String = contents.Split("|")
        MessageBox.Show(SplitContents(0) & Environment.NewLine & SplitContents(1))

        If SplitContents(0) = TxtUsername.Text AndAlso SplitContents(1) = txtPassword.Text Then
            'Passed
            Edit_Competitor.Show()
        Else
            'Failed
            MsgBox("incorrect Username/password.")
        End If

in the text file:
VB.NET:
Ben
Ben|Ash|
Ben|Ash|
Ben|Ash|
||
Ben|Ash|
Ben|Ash|

a few failed attempts in there..
 
It's the first line in the text file, there isn't a SplitContents(1) because that line doesn't have a "|" on it.

It's easy to correct this, check the length property of the array after creating it on the split(), if it's greater than 2, proceed with the code, otherwise skip it. Of course, you will need to have a loop because right now you're only using the 1st line.

Also change the "contents = i.Read()" to "contents = i.ReadLine()"
 
Back
Top