×

VB6快速读取txt

hqy hqy 发表于2025-02-21 16:12:35 浏览18 评论0

抢沙发发表评论

一、快速读取txt,空白内容则会出错。

1、建立模版

Function GetFile(FileName As String) As String

Dim i As Integer, s As String, BB() As Byte

If Dir(FileName) = "" Then Exit Function

i = FreeFile

ReDim BB(FileLen(FileName) - 1)

Open FileName For Binary As #i

Get #i, , BB

Close #i

s = StrConv(BB, vbUnicode)

GetFile = s

End Function

?复制代码

2、窗体调用举例:

dim s as string

s=GetFile("c:\1.txt")

Text1 = s

?复制代码

3、如果文件中没中文字符,那么下面代码读整个文件速度是非常快的,我读了一个6M文件,只用了不到1秒,如果有中文字符,就需要把减去中文字符数量。

s = Input$(LOF(1), #1)中的LOF(1)  

Dim r As String, s As String

r = Environ("userprofile")

r = r + ""c:\1.txt"    

Open r For Input As #1

s = Input$(LOF(1), #1)

Debug.Print s

Close #1

?复制代码

二、(另外一种方法)空白文件照样读取,这种方法比较好。

s = App.Path '当前目录

 Open s & "\rootCA.log" For Input As #1

Do While Not EOF(1)

Input #1, b

Text7.Text = Text7.Text & b

Loop

Close #1

?复制代码

三、(第三种方法)一行代码读入,空白内容也会出错。

Text1 = CreateObject("scripting.filesystemobject").opentextfile("C:\1.txt").readall

?复制代码

四、数据查找InStr含数

If InStr(Text1, "ok") >= 1 Then

MsgBox "存在OK关键字", 64, "提示"

Else

MsgBox "不存在Ok关键字", 16, "提示"

End If


打赏

本文链接:https://www.kinber.cn/post/4927.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客