VB读txt文本问题

tld12 2008-07-16 03:36:57
我想让它每回只读txt文本里的一行数据,而不是全读,进行相关操作以后再读下一条该怎么办,才学VB两天,请大家帮帮忙
...全文
134 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hansuzhi 2008-07-17
  • 打赏
  • 举报
回复
使用FileSystemObject,TextStream
需要添加引用:Microsoft Scripting Runtime
代码如下:

Dim fso As New FileSystemObject
Dim fileName As String
Dim ts As TextStream
Dim strReadLine As String

fileName = "FilePath" ' 输入你的 txt文件路径
If fso.FileExists(fileName) Then
Set ts = fso.OpenTextFile(fileName, ForReading, False)
While Not ts.AtEndOfStream
strReadLine = ts.ReadLine
Debug.Print strReadLine
If Trim(strReadLine) <> "" Then
'这里可以添加你的处理代码
else
'可以使用下面语句跳过一行
ts.SkipLine
End If
Wend
set ts=nothing
End if
setfso=nothing
goosen 2008-07-17
  • 打赏
  • 举报
回复

Option Explicit

Private Sub Command1_Click()
Dim sFile As String
Open "d:\1.txt" For Input As #1 '打开你的文件
Do
Line Input #1, sFile
'在这里添加你的处理代码
MsgBox sFile '处理后一行一行读出
Loop While Not (EOF(1))
Close #1

End Sub
fskjb01 2008-07-16
  • 打赏
  • 举报
回复
用line input语句不知道符合楼主的意识吗!!
hhjjhh009 2008-07-16
  • 打赏
  • 举报
回复
mid("数据",1,5)'代表从第一个字符截取到第五个字符

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧