473,289 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Reading in a textfile?

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?

Mar 1 '07 #1
16 1483
On Mar 1, 12:48 pm, "Ron" <pts4...@yahoo.comwrote:
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

Mar 1 '07 #2
rowe_newsgroups wrote:
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...

--

(O)enone
Mar 1 '07 #3
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.
------------------------------

"Ron" <pt*****@yahoo.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
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?


Mar 1 '07 #4
RobinS wrote:
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")
///

--

(O)enone
Mar 1 '07 #5
Same thing, just different. ;-)

Robin S.
------------------------------------
"(O)enone" <oe****@nowhere.comwrote in message
news:0c*******************@newsfe6-gui.ntli.net...
RobinS wrote:
>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")
///

--

(O)enone

Mar 1 '07 #6
Ron wrote:
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(sFile Name)
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.
Mar 2 '07 #7
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.
Mar 2 '07 #8
Phill W. wrote:
Personally, I wouldn't do it this way.
Neither would I -- but the way I'd do it would be to use a database...

--

(O)enone
Mar 2 '07 #9
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
Mar 2 '07 #10

"Oenone" <oe****@nowhere.comwrote in message
news:O5**************@TK2MSFTNGP02.phx.gbl...
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
Mar 2 '07 #11
RobinS wrote:
"Oenone" <oe****@nowhere.comwrote in message
news:O5**************@TK2MSFTNGP02.phx.gbl...
>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. :)

--
Göran Andersson
_____
http://www.guffa.com
Mar 2 '07 #12
"Göran Andersson" <gu***@guffa.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
RobinS wrote:
>"Oenone" <oe****@nowhere.comwrote in message
news:O5**************@TK2MSFTNGP02.phx.gbl...
>>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. :)

--
Göran Andersson
_____
http://www.guffa.com

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.
Mar 2 '07 #13

"Göran Andersson" <gu***@guffa.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
RobinS wrote:
>"Oenone" <oe****@nowhere.comwrote in message
news:O5**************@TK2MSFTNGP02.phx.gbl...
>>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. :)

--
Göran Andersson
_____
http://www.guffa.com
--------------------------------

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.
Mar 2 '07 #14
RobinS wrote:
Do you think WriteAllLines is more performant than concatenating all
of your lines together and then doing WriteAllText?
Yes.

--

(O)enone
Mar 2 '07 #15
RobinS wrote:
"Göran Andersson" <gu***@guffa.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
>RobinS wrote:
>>"Oenone" <oe****@nowhere.comwrote in message
news:O5**************@TK2MSFTNGP02.phx.gbl...
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. :)

--
Göran Andersson
_____
http://www.guffa.com


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
_____
http://www.guffa.com
Mar 3 '07 #16
"Göran Andersson" <gu***@guffa.comwrote in message
news:OH**************@TK2MSFTNGP04.phx.gbl...
RobinS wrote:
>"Göran Andersson" <gu***@guffa.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
>>RobinS wrote:
"Oenone" <oe****@nowhere.comwrote in message
news:O5**************@TK2MSFTNGP02.phx.gbl...
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. :)

--
Göran Andersson
_____
http://www.guffa.com


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
_____
http://www.guffa.com
Good point. Thanks!
Robin
Mar 3 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Anders Eriksson | last post by:
Hello! I'm a beginner at PHP and I wonder how I would go about to read a textfile into an array(hash). The textfile looks like this: A/S=age/sex? A/S/L=age/sex/location? AA=alcoholics...
2
by: Suchi | last post by:
Hi all: I want to read a textfile from an ASP program. My program is like this: Set fso = CreateObject("Scripting.FileSystemObject") workFile=http://localhost/Readme.txt) Set textFile =...
2
by: BjoernJackschina | last post by:
Hello, I just look for a capability to sort several words in view of alphabet. An example: stop is 'opst' reach is 'aechr' This should read in a new file so that I can look for same letter...
16
by: Roy | last post by:
I use a Access 2K application.I am trying to use Chuck Grimsby(clsReadTextFile.txt)class utility to read a text file and then do a import of the same into my database.The question is how to call...
2
by: chris | last post by:
Hi there, I am reading in a textfile which looks like this (there is no new line after the last number) 03 98661881 0407 566453 The code to load the textfile looks like this:
2
by: novacreatura | last post by:
Hi, I have a project that's supposed to create a program for a "Dating Service". The first part of the program is to read a textfile of profiles which include names, age, etc...into a string...
1
by: Justin Fancy | last post by:
Hi everyone, I have a textfile which I need to read and compare dates. The text file summarizes every time I do an update to an internet site. Sample output is as follows: Copying...
6
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data...
3
by: Arn | last post by:
I just started to learn C++ and have some problem when I read a hole line from a textfile. I would be grateful if anyone can tell me what is wrong with my code. I'm using Borland Developer Studio...
1
by: asedt | last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile. My problem is that i don't know how to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.