QTP可以在txt文件(文本文件中读取数据)
首先创造一个文档对象 set fso = createObject("scripting.filesystemobject")
然后用此对象打开目标文档 Set txt = fso.OpenTextFile( "C:\Documents and Settings\Administrator\桌面\test.txt",8,true)
这里说一说OpenTextFile方法,根据QTP的帮助文档中记载
根据以上帮助文档记录,我们现在要实现的是读取一个txt文档,OpenTextFile方法里的iomode模式采用只读模式(ForReading|1)代码如下:
Option explicit
Dim fso
set fso = createObject("scripting.filesystemobject")
Dim txt
Set txt = fso.OpenTextFile( "C:\Documents and Settings\Administrator\桌面\test.txt",1,true)
'Skips the next line when reading a TextStream file.忽略掉文档中的第一行
'txt.SkipLine
Dim content
While not txt.AtEndOfStream
'读取txt文档中的一行
content = txt.ReadLine
print(content)
print("------------------------")
Wend
txt.Close
Set txt = nothing
Set fso = nothing
好了,上述操作即实现了对txt文档的读取操作。
接着,我们实现向txt文档写入内容的操作,iomode采用写入的模式ForWriting|2和ForAppending|8都可以。实现代码如下:
Option explicit
Dim fso
set fso = createObject("scripting.filesystemobject")
Dim txt
Set txt = fso.OpenTextFile( "C:\Documents and Settings\Administrator\桌面\test.txt",8,true)
txt.WriteLine("This is a new line")
txt.Close
Set txt = nothing
Set fso = nothing