你笑的时候全世界陪你一起笑,你哭的时候只有你一个人哭

使用Robot从txt文件中读取不同行的内容并显示(续)

上一篇 / 下一篇  2006-12-14 22:02:33 / 个人分类:Rational Robot 技术

上一篇文章中介绍了如何使用Robot编写函数,取得txt文件中每一行的内容,但给出的脚本中还存在几个问题:1.函数没有提取出来,共用性不强 2.如果想从一个有n多行的文件中把所有内容取出,则每调用一次函数,函数内部都会跑一遍循环取值,很浪费

于是我又重新整理了一下,将函数放置到库文件(.sbl)中,并在里面定一一个全局变量cell(),取值函数ReturnCell变为子程序,删除返回值的参数,这个子程序中将取得的变量放到数组cell中,这样引用这个sbl的脚本就可以使用cell()的值了。脚本如下:

主脚本:txt

'$include: "FunctionLib.sbl"

Sub main                  '主脚本
    Dim x as integer
    Dim Location as string
    Dim msgtext as string
   
    Location = "D:\gvb.txt"    '指定一个文件
   
    x = returnrownum(location)    '调用ReturnRowNum函数,返回这个文件中的行数
   
    call ReturnCell(location,x)    '调用ReturnCell,返回指定文件,指定行的内容
    msgbox cell(0)
    msgbox cell(3)

End sub

==============================================

FunctionLib.sbl

Global cell()         '全局变量,动态数组

Function ReturnRowNum(Dircetory as string) as integer    '返回指定文件的行数
   Dim testscore as String
   Dim x,i,y as integer
  
   if IsMissing(Dircetory) then
    msgbox "no Dircetory"
   End if
  
   Open Dircetory for Input as #1
   x=0
   i=0
   Do Until x=Lof(1)
      Line Input #1, testscore
      x=x+1
      i=i+1
      y=Seek(1)
      If y>Lof(1) then
         x=Lof(1)
      Else
         Seek 1,y
      End If
   Loop
   Close #1
   ReturnRowNum = i
End Function  

Sub ReturnCell(Dircetory as string,RowNum as integer)  '子程序
   Dim testscore as String
   Dim x,i,y as integer 
   if IsMissing(Dircetory) then
    msgbox "no Dircetory"
   End if
   if IsMissing(RowNum) then
    RowNum = 0
   End if
       
   Redim cell(RowNum-1)
   Open Dircetory for Input as #1
   x=0
   Do Until x=Lof(1)
      Line Input #1, testscore
      cell(x) = testscore
      x=x+1
      y=Seek(1)
      If y>Lof(1) then
         x=Lof(1)
      Else
         Seek 1,y
      End If 
   Loop
 
   Close #1
 
End Sub

 


TAG:

 

评分:0

我来说两句

Open Toolbar