阅读文本文件? [英] Reading in a textfile?

查看:67
本文介绍了阅读文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我已经创建了一个正常运行的ATM程序,一台银行机器。


现在我想实现用户名和针脚。所以我有一个带有这个信息的文件

文件。

BILL,1111

TOM,2222

CHRIS, 3333


如何从文本文件中读取这些值到数组

然后访问这些数组?

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?

推荐答案

3月1日下午12:48,Ron < pts4 ... @ yahoo.comwrote:
On Mar 1, 12:48 pm, "Ron" <pts4...@yahoo.comwrote:

大家好,


我创建了一个功能正常的ATM程序,银行机器。


现在我想实现用户名和Pins。所以我有一个带有这个信息的文件

文件。

BILL,1111

TOM,2222

CHRIS, 3333

如何将这些值从文本文件读入数组

然后访问这些数组?
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?



查找文件流。


另外,我强烈建议使用Xml文件而不仅仅是文本

档。


类似于:


< Accounts>

< Users>

< Bill pin =" 1111" />

< Tom pin =" 2222" />

< Chris pin =" 3333" />

< / User>

< / Accounts>


然后使用类似这样的内容验证用户


''Imports System.Xml假设


私有函数IsValid(userName为字符串,userPin为字符串)

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
结束如果

结束功能


谢谢,


Seth Rowe

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写道:
rowe_newsgroups wrote:

查找文件流。
Look up filestream.



或者如果您使用的是VB2005,请查看System.IO.File.ReadAllLines,它将读取每行的
将文本文件转换为字符串数组的单独元素,所有

只需一行代码。可爱。 :)

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. :)


另外,我强烈建议使用Xml文件,而不仅仅是文本

文件。

类似于:


< Accounts>

< Users>

< Bill pin =" 1111 QUOT; />

< Tom pin =" 2222" />

< Chris pin =" 3333" />

< / User>

< / Accounts>
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>



....虽然如果你使用XML,我会更像这样构造:


< ;帐户>

<用户>

<用户名=" Bill"销= QUOT; 1111" />

< User name =" Tom"销= QUOT; 2222" />

< User name =" Chris"销= QUOT; 3333" />

< / Users>

< / Accounts>


....或者像这样:


< Accounts>

< Users>

< User>

< Name> ; Bill< / Name>

< Pin> 1111< / Pin>

< / User>

< User>

<名称> Tom< /名称>

< Pin> 2222< / Pin>

< / User>

<用户>

<名称> Chris< /名称>

< Pin> 3333< / Pin>

< ; /用户>

< /用户>

< / Accounts>


.... XML有一些严格有关元素命名的规则,并且你不希望
想要到达项目的最后并发现你不能存储名称

带空格in或者使用诸如O''Brien之类的撇号。


您可能还想考虑加密PIN ...


-


(O)enone

....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


如果您不能像其他人讨论的那样将文件转换为XML,您可以

读取整个文件然后遍历每一行并解析它。


Dim crlfs()as String = {ControlChars.CrLf}

Dim lines()as String = File.ReadAllText(" c:\\ \\ _data.txt")。分割(crlfs,

StringSplitOptions.None)

Dim numOfLines = lines.Length


然后通过每一行并对其进行拆分。


我的整数= 0到lines.length - 1

''使用string.split这里的功能是将行(i)分成一个数组

next


您可以使用二维数组来保存结果。我可能会使用一个

字典,名称是关键,而引脚就是值。


如果你想这样做并且无法弄清楚我在说什么,

回复后我会发布一些代码。


Robin S. br />
------------------------------


"罗恩" < pt ***** @ yahoo.comwrote in message

news:11 ********************** @ 8g2000cwh。 googlegrou ps.com ...
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...

大家好,


我创建了一个正常运行的ATM程序,一个银行机器。


现在我想实现用户名和Pins。所以我有一个带有这个信息的文件

文件。

BILL,1111

TOM,2222

CHRIS, 3333


如何从文本文件中读取这些值到数组

然后访问这些数组?
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?






这篇关于阅读文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆