Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » .NET Programming » Visual Basic .NET » Reading a Multi-line Text file


Reading a Multi-line Text fileExpand / Collapse
Author
Message
Posted 6/16/2009 6:05:18 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 6/17/2009 1:28:19 PM
Posts: 1, Visits: 4
Hi A1 forums,

I've got a small issue with a program I'm working on. In a nutshell, the user enters two values, their name and a number. Those values are written to a text file. I want it to then be able to have the user re-enter their name, and have it check for said name in the text file. The code I have so far works...somewhat.

(I apologize for the bad formatting, if there's any way for me to make it a little less Wall-of-Texty I'll be happy to edit my post)

read = File.OpenText("Codes.txt")

name1 = InputBox("Enter your name.", "Name")
namein = read.ReadLine()

If namein.ToUpper <> name1.ToUpper And tf = False Then
namein = read.ReadLine()
MessageBox.Show(namein & " " & pass, "Debug box", MessageBoxButtons.OK)
ElseIf namein.ToUpper = name1.ToUpper Then
pass = CInt(read.ReadLine())
passin = InputBox("Enter your password.", "Enter")
If pass = passin Then
MessageBox.Show("Password Verified.", "Password")
Else
MessageBox.Show("Password Incorrect, please try again.", "Password Error.", MessageBoxButtons.OK)
End If
MessageBox.Show(namein & " " & pass, "Debug box", MessageBoxButtons.OK)
End If

For the sake of an argument "Codes.txt" contains the following line of text:

Bob, 12.5

My main issue is separating the "Bob" and "12.5" as right now the code is bringing the entire line in as a variable (line in bold). Is there any way for me to separate the name and the code? Thanks a ton for any help that you can give
Post #27803
Posted 6/18/2009 12:09:19 PM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 10/16/2009 10:14:44 AM
Posts: 38, Visits: 45
Is the data on each line delimited by a space or comma? How about reading in the entire line and then filter out the number using the .substring and .IndexOf methods?

strName = strInput.Substring(0, strInput.IndexOf(",")) 'assuming data separated by comma

Post #27814
Posted 6/29/2009 3:54:43 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 6/4/2010 11:22:33 AM
Posts: 633, Visits: 1,018
First, the Split() function is great for separating strings at a delimiter. Use this when you're assigning the ReadLine() value to namein

Also, you'll only read the top line of your text file. You'll want to use a loop like this:

Do Until EOF(1)

Read...

Loop

"1" would be the integer you assign to a file using the FileOpen function, which in your case I would advise you use.

Lastly, for formatting, use Mark's tool. Most A1 gods and gurus do:

http://www.a1vbcode.com/a1vbcode/vbforums/Attachment30.aspx



-Mobius

Post #27851
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Admin, Mod

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 6:35pm