Reading in a textfile?

  • Thread starter Thread starter Ron
  • Start date Start date
R

Ron

Hello everyone,

I've created a functioning ATM program, a bank machine.

Now I want to implement usernames and Pins into it. So I have a text
file with this info.
BILL, 1111
TOM, 2222
CHRIS, 3333

How do I go about reading those values from a text file into arrays
and then access those arrays?
 
Hello everyone,

I've created a functioning ATM program, a bank machine.

Now I want to implement usernames and Pins into it. So I have a text
file with this info.
BILL, 1111
TOM, 2222
CHRIS, 3333

How do I go about reading those values from a text file into arrays
and then access those arrays?

Look up filestream.

Also, I would highly suggest using a Xml file instead of just a text
file.

Something like:

<Accounts>
<Users>
<Bill pin="1111" />
<Tom pin="2222" />
<Chris pin="3333" />
</User>
</Accounts>

Then use something like this to validate users

'Imports System.Xml assumed

private function IsValid(userName as string, userPin as string)
Dim doc as XmlDocument
doc.Load("MyXmlFilePath.Xml")
Dim node as XmlNode = doc.GetElementsByTagName(userName)(0)
if node.Attributes("pin").Value = userPin then
return True
else
return false
end if
end function

Thanks,

Seth Rowe
 
rowe_newsgroups said:
Look up filestream.

Or if you're using VB2005, look up System.IO.File.ReadAllLines, which will
read each line of a text file into separate elements of a string array, all
in just one line of code. Lovely. :)
Also, I would highly suggest using a Xml file instead of just a text
file.

Something like:

<Accounts>
<Users>
<Bill pin="1111" />
<Tom pin="2222" />
<Chris pin="3333" />
</User>
</Accounts>

....although if you do use XML, I'd structure it more like this:

<Accounts>
<Users>
<User name="Bill" pin="1111" />
<User name="Tom" pin="2222" />
<User name="Chris" pin="3333" />
</Users>
</Accounts>

....or like this:

<Accounts>
<Users>
<User>
<Name>Bill</Name>
<Pin>1111</Pin>
</User>
<User>
<Name>Tom</Name>
<Pin>2222</Pin>
</User>
<User>
<Name>Chris</Name>
<Pin>3333</Pin>
</User>
</Users>
</Accounts>

....XML has some strict rules about the naming of elements, and you don't
want to get to the end of your project and find that you can't store names
with spaces in, or with apostrophes such as in "O'Brien".

You might also want to consider encryption of the PINs...
 
If you can't convert the file to XML as others have discussed, you could
read the whole file in and then go through each line and parse it.

Dim crlfs() as String = {ControlChars.CrLf}
Dim lines() as String = File.ReadAllText("c:\data.txt").Split(crlfs,
StringSplitOptions.None)
Dim numOfLines = lines.Length

Then go through each line and do a split on it.

For i as Integer = 0 to lines.length - 1
'use the string.split function here to split lines(i) into an array
next i

You could use a 2-D array to hold the results. I'd probably use a
Dictionary, with the name being the key, and the pin being the value.

If you want to go this way and can't figure out what I'm talking about,
post back and I'll post some code.

Robin S.
 
RobinS said:
If you can't convert the file to XML as others have discussed, you
could read the whole file in and then go through each line and parse
it.
Dim crlfs() as String = {ControlChars.CrLf}
Dim lines() as String = File.ReadAllText("c:\data.txt").Split(crlfs,
StringSplitOptions.None)

=

\\\
Dim lines() As String = File.ReadAllLines("c:\data.txt")
///
 
Ron said:
Hello everyone,

I've created a functioning ATM program, a bank machine.

Now I want to implement usernames and Pins into it. So I have a text
file with this info.
BILL, 1111
TOM, 2222
CHRIS, 3333

How do I go about reading those values from a text file into arrays
and then access those arrays?

Or, you could use my favourite -

Using MyReader As New
Microsoft.VisualBasic.FileIO.TextFieldParser(sFileName)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim sUserData As String()
While Not MyReader.EndOfData
Try
sUserData = MyReader.ReadFields
'sUserData(0)=Username / sUserData(1)=PIN
'(Assign to your Main Array)
Catch ex As Exception
'This will happen on an Invalid Import Line.
End Try
End While
End Using

Just another option (rather than using XML).

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
Ron wrote:

Now I want to implement usernames and Pins into it. So I have a text
file with this info.
BILL, 1111
TOM, 2222
CHRIS, 3333

How do I go about reading those values from a text file into arrays
and then access those arrays?

Personally, I wouldn't do it this way.

/If/ you had 100,000 users, you'd load all 100,000 usernames and
passwords into [a /lot/ of] memory, but only use perhaps one or two of
them.

If you want to stay with the file for holding your data then scan it for
a /particular/ user as and when you need to.

Imports VB=Microsoft.VisualBasic ' see below

Function CheckPassword( _
ByVal sUser as String _
ByVal sPassword as String _
) as Boolean

Dim sr as New StreamReader( "file" )
Dim sSearch as String = sUser.ToUpper() & ", "

Try
Dim sLine as String = sr.ReadLine()

Do While Not (sLine Is Nothing)
if sLine.StartsWith(sSearch) then
Dim sPswd As String _
= VB.Split(sLine, ", ")(1).Trim() ' multi-char delimiter

If sPswd = sPassword Then
Return True
End if

end if
sLine = sr.ReadLine()
Loop
Finally
sr.Close()
End Try

Return False

End Function

.... then ...

If CheckPassword( "Chris", "3333" )
...

HTH,
Phill W.
 
Oenone said:
I know -- but it's always nice to know more efficient ways to do the same
thing (well I think so, anyway :-).

It's okay, it's always good to have options. I'm not sure which way is more
efficient, or if there's any difference. Maybe ReadAllLines does the same
thing as my code. It's one line of code shorter, that's for sure. ;-)

Robin
 
RobinS said:
It's okay, it's always good to have options. I'm not sure which way is more
efficient, or if there's any difference. Maybe ReadAllLines does the same
thing as my code. It's one line of code shorter, that's for sure. ;-)

Robin

The ReadAllLines method is implemented like this:

public static string[] ReadAllLines(string path, Encoding encoding) {
ArrayList list1 = new ArrayList();
using (StreamReader reader1 = new StreamReader(path, encoding)) {
string text1;
while ((text1 = reader1.ReadLine()) != null) {
list1.Add(text1);
}
}
return (string[]) list1.ToArray(typeof(string));
}

It uses roughly half the amount of memory as reading it all as a string
and splitting it. :)
 
Göran Andersson said:
RobinS said:
It's okay, it's always good to have options. I'm not sure which way is
more efficient, or if there's any difference. Maybe ReadAllLines does
the same thing as my code. It's one line of code shorter, that's for
sure. ;-)

Robin

The ReadAllLines method is implemented like this:

public static string[] ReadAllLines(string path, Encoding encoding) {
ArrayList list1 = new ArrayList();
using (StreamReader reader1 = new StreamReader(path, encoding)) {
string text1;
while ((text1 = reader1.ReadLine()) != null) {
list1.Add(text1);
}
}
return (string[]) list1.ToArray(typeof(string));
}

It uses roughly half the amount of memory as reading it all as a string
and splitting it. :)


Thank you! I'll store that in my file next to the ReadAllText.

Do you think WriteAllLines is more performant than concatenating all of
your lines together and then doing WriteAllText?

Dim newText as String = _
String.Join(ControlChars.CrLf, lines, 1, lines.length - 1)
File.WriteAllText("c:\output.txt", newText, False)

Robin S.
 
Göran Andersson said:
RobinS said:
It's okay, it's always good to have options. I'm not sure which way is
more efficient, or if there's any difference. Maybe ReadAllLines does
the same thing as my code. It's one line of code shorter, that's for
sure. ;-)

Robin

The ReadAllLines method is implemented like this:

public static string[] ReadAllLines(string path, Encoding encoding) {
ArrayList list1 = new ArrayList();
using (StreamReader reader1 = new StreamReader(path, encoding)) {
string text1;
while ((text1 = reader1.ReadLine()) != null) {
list1.Add(text1);
}
}
return (string[]) list1.ToArray(typeof(string));
}

It uses roughly half the amount of memory as reading it all as a string
and splitting it. :)
--------------------------------

I just realized you wrote that in C#. I think the fact that I can read that
without thinking twice about it makes me ambidexterous. :-D

Robin S.
 
RobinS said:
Göran Andersson said:
RobinS said:
RobinS wrote:
Same thing, just different. ;-)
I know -- but it's always nice to know more efficient ways to do the
same thing (well I think so, anyway :-).

--

(O)enone
It's okay, it's always good to have options. I'm not sure which way is
more efficient, or if there's any difference. Maybe ReadAllLines does
the same thing as my code. It's one line of code shorter, that's for
sure. ;-)

Robin
The ReadAllLines method is implemented like this:

public static string[] ReadAllLines(string path, Encoding encoding) {
ArrayList list1 = new ArrayList();
using (StreamReader reader1 = new StreamReader(path, encoding)) {
string text1;
while ((text1 = reader1.ReadLine()) != null) {
list1.Add(text1);
}
}
return (string[]) list1.ToArray(typeof(string));
}

It uses roughly half the amount of memory as reading it all as a string
and splitting it. :)


Thank you! I'll store that in my file next to the ReadAllText.

Do you think WriteAllLines is more performant than concatenating all of
your lines together and then doing WriteAllText?

Dim newText as String = _
String.Join(ControlChars.CrLf, lines, 1, lines.length - 1)
File.WriteAllText("c:\output.txt", newText, False)

Robin S.

Yes. By concatenating the lines you create another copy of the entire
file in memory. WriteAllLines doesn't do that, so it scales much better.
 
Göran Andersson said:
RobinS said:
Göran Andersson said:
RobinS wrote:
RobinS wrote:
Same thing, just different. ;-)
I know -- but it's always nice to know more efficient ways to do the
same thing (well I think so, anyway :-).

--

(O)enone
It's okay, it's always good to have options. I'm not sure which way is
more efficient, or if there's any difference. Maybe ReadAllLines does
the same thing as my code. It's one line of code shorter, that's for
sure. ;-)

Robin
The ReadAllLines method is implemented like this:

public static string[] ReadAllLines(string path, Encoding encoding) {
ArrayList list1 = new ArrayList();
using (StreamReader reader1 = new StreamReader(path, encoding)) {
string text1;
while ((text1 = reader1.ReadLine()) != null) {
list1.Add(text1);
}
}
return (string[]) list1.ToArray(typeof(string));
}

It uses roughly half the amount of memory as reading it all as a string
and splitting it. :)


Thank you! I'll store that in my file next to the ReadAllText.

Do you think WriteAllLines is more performant than concatenating all of
your lines together and then doing WriteAllText?

Dim newText as String = _
String.Join(ControlChars.CrLf, lines, 1, lines.length - 1)
File.WriteAllText("c:\output.txt", newText, False)

Robin S.

Yes. By concatenating the lines you create another copy of the entire
file in memory. WriteAllLines doesn't do that, so it scales much better.

Good point. Thanks!
Robin
 
Back
Top