用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.doczj.com/doc/1b5d369e51e79b8968022637.html /BNClists/lemma.al”, “lemma.al”,0)
    $file=FileOpen(“lemma.al”,0)
    While1
    $line=FileReadLine($file)
    If@error=-1Then ExitLoop
    $lineElements=StringSplit($line,” “)
    If AddUpCharactersInWordAsNumber($lineElements[3])
    ==100Then
    ConsoleWrite($lineElements[3]&@CRLF) EndIf
    WEnd
    FileClose($file)
    MsgBox(0,””,”Finished!”)
    Func AddUpCharactersInWordAsNumber($word)
    $wordLowerCase=StringLower($word)
    $Characters=StringSplit($wordLowerCase,””)
    $count=0
    For$i=1To$Characters[0]
    $count=$count+Asc($Characters[$i])-96 Next
    Return$count
    按F5查看一下运行结果。
    再隔一篇文章,我才会详细解释这些代码──因为还有一些细节需要交代。
    让我们先了解一下AutoIt是怎样进行文件读写的。请阅读一下以下代码:
    $file=FileOpen(“lemma.al”,0)
    While1
    $line=FileReadLine($file)
    If@error=-1Then 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中,1和True是一回事(暂时别奇怪你为什么搞不清楚这句话的因果关系,反正记住1和True是一回事就行了)──都可以当作表达式,且它们的值是相同的。
    所以,…
    WEnd
    是个无限循环。While 1 … WEnd之间的代码会无穷无尽地重复执行下去,除非……
    请注意这行代码:
    If@error=-1Then 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)
    While1
    $line=FileReadLine($file)
    If@error=-1Then 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()帮助文档。