AutoIt教程:7. 文件读写

by xiaolai on 2010/05/02 · 5 comments

in Auto-It

用AutoIt可以很方便地读写文本文件。

新建一个AutoIt文件,将其命名为Testing16.au3,输入以下代码:

;Once I read in an article:
;
; Take a piece of paper, right now, and write out the word ATTITUDE.
; Now assign each letter the number that letter corresponds to and
; add those numbers up. Guess what, they add up to 100%. A=1, T=20,
; T=20, I=9, T=20, U=21, D=4, E=5 = 100%. Attitude is everything folks.
; You can’t hide it; you can’t even deny it.;
; It’s who you are. It’s the outside of your inside.
;
;I thought, Oh! Really?
;Seconds later, I became suspicious of this claim...
;OK, let's find out the truth, since we have tools to utilize...
 
InetGet("http://www.kilgarriff.co.uk/BNClists/lemma.al", "lemma.al", 0)
 
$file = FileOpen("lemma.al", 0)
While 1
	$line = FileReadLine($file)
	If @error = -1 Then ExitLoop
	$lineElements = StringSplit($line, " ")
	If AddUpCharactersInWordAsNumber($lineElements[3]) == 100 Then
		ConsoleWrite($lineElements[3] & @CRLF)
	EndIf
WEnd
FileClose($file)
 
MsgBox(0, "", "Finished!")
 
Func AddUpCharactersInWordAsNumber($word)
	$wordLowerCase = StringLower($word)
	$Characters = StringSplit($wordLowerCase, "")
	$count = 0
	For $i = 1 To $Characters[0]
		$count = $count + Asc($Characters[$i]) - 96
	Next
	Return $count
EndFunc

按F5查看一下运行结果。

再隔一篇文章,我才会详细解释这些代码──因为还有一些细节需要交代。

让我们先了解一下AutoIt是怎样进行文件读写的。请阅读一下以下代码:

$file = FileOpen("lemma.al", 0)
While 1
	$line = FileReadLine($file)
	If @error = -1 Then ExitLoop
	...
WEnd
FileClose($file)

查看帮助文档的话,它告诉你说,FileOpen()这个函数返回一个File “Handle”(翻译成中文,是一个不知所云的词组“文件句柄”)。实际上,还不如干脆把这一行代码完整翻译成自然语言:

$file = FileOpen("lemma.al", 0)

相当于:

从此之后,我们就可以用变量$file指代那个以只读模式打开并保存于内存中的”lemma.al”文件。[1]

While后面的1是怎么回事儿?

帮助文件里,While…WEnd的示例是这么写的:

While
    statements
    ...
WEnd

While后面接一个表达式,即,“当这个表达式为真之时,循环执行以下代码……

事实上,对AutoIt来说,1也是一个表达式,这个表达式的运算结果就是1。AutoIt从设计初始,就被定义为“弱类型”语言,即,对数据类型的定义不严格(至于这句话究竟是什么意思,以后的文章中会详细解释)。于是,在AutoIt中,1True是一回事(暂时别奇怪你为什么搞不清楚这句话的因果关系,反正记住1True是一回事就行了)──都可以当作表达式,且它们的值是相同的。

所以,

While 1
...
WEnd

是个无限循环。While 1 … WEnd之间的代码会无穷无尽地重复执行下去,除非……

请注意这行代码:

If @error = -1 Then ExitLoop

在循环内部,每次执行

$line = FileReadLine($file)

的时候,FileReadLine()都从$file里读出一行内容,并保存到变量$line之中($line就成了一个字符串变量)……

FileReadLine()的帮助文档里写着:

Return Value
Success: Returns a line of text.
Special: Sets @error to -1 if end-of-file is reached.

也就是说,当读到文件结尾的时候,这个函数会把@error的值设置为-1

这里有两个东西要解释:

  1. end-of-file,又常常被缩写为EOF。它是所有文本文件的末尾的一个用记事本打开看不到的字符(文本文件中用记事本打开看不到的字符有很多,比如换行符Carriage Return Line Feed……)。当FileReadLine()读到这个字符之时,它就“知道”文件已经全部读取完毕了。
  2. @error,是AutoIt内建的一个“宏”,它的初始值是0。你不妨新建一个AutoIt文件,输入一行代码:
      ConsoleWrite(@error)

      看看@error的初始值到底是什么……

所以,以下代码:

$file = FileOpen("lemma.al", 0)
While 1
	$line = FileReadLine($file)
	If @error = -1 Then ExitLoop
	ConsoleWrite($line)
WEnd
FileClose($file)

的意思是说:

  1. 以“读模式”打开lemma.al文件;
  2. 逐行读取lemma.al文件内容;
  3. 将每一行内容输出至控制台;
  4. 关闭lemmal.al文件。

作业

  1. Testing16.au3里用了好几个“陌生”的函数,请逐一去查阅相关文档。
  2. 尝试理解每一行代码,并分析整个程序的运行流程。

Footnotes:

  1. FileOpen()的第二个参数是用来指定文件读取模式的,0为读模式,1为写模式……具体请参阅FileOpen()帮助文档。 []

{ 5 comments… read them below or add one }

l9yuan May 2, 2010 at 11:39

按F5运行后,会出现Au3Check errors的对话框,其中显示:
C:\Documents and Settings\user\Desktop\testing16.au3.au3(22,45) : ERROR: syntax error
ConsoleWrite($lineElements[3] & @CRLF)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\user\Desktop\testing16.au3.au3(29,1) : ERROR: missing EndIf.
Func
^
C:\Documents and Settings\user\Desktop\testing16.au3.au3(21,64) : REF: missing EndIf.
If AddUpCharactersInWordAsNumber($lineElements[3]) == 100 Then
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\user\Desktop\testing16.au3.au3(29,1) : ERROR: missing Wend.
Func
^
C:\Documents and Settings\user\Desktop\testing16.au3.au3(17,1) : REF: missing Wend.
While
^
C:\Documents and Settings\user\Desktop\testing16.au3.au3 – 3 error(s), 0 warning(s)

Reply

l9yuan May 2, 2010 at 11:46

sorry, 我已经解决了问题。
我觉得您原文代码中第22行,应由:
ConsoleWrite($lineElements[3] & @CRLF)
改为:
ConsoleWrite($lineElements[3] @CRLF)
否则无法正确运行
p.s. 我用的版本是AutoIt Version: 3.3.4.0

Reply

李笑来 May 2, 2010 at 15:25

是贴成html的时候出了问题,&变成了&

Reply

hzjhds May 2, 2010 at 19:10

笑来老师能否附带介绍一下在实际应用的案例和背景以利于我们更深入的理解

Reply

呵呵 September 14, 2011 at 20:32

感觉对于fileopenline这个函数,有必要说一下:
FileReadLine ( “filehandle/filename” [, line] )
line参数可加,可不加。如果不加,该函数默认是一行接一行的读取。所以如果每行都读取的话,无需写行号,写了反而会让函数从头开始数,降低效率。
这一点和普通编程语言的读取函数不同。我是刚开始接触autoit,有点想当然了,所以刚开始看这个程序有点迷惑,查了帮助才知道。写在这里,让新手们注意下。

Reply

Leave a Comment

Previous post:

Next post: