如何读取txt文件中的任意一行数据?

why_wxl 2002-06-28 08:32:55
注意用open(),读取指定的行,而不是字节!
...全文
405 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qhzxcz 2002-06-29
  • 打赏
  • 举报
回复
Public Function ReadDataFromTxtFile(ByVal MyTextFile As String) As String
'功能说明:文本文件(MyTextFile)写入(MyData)中

'写入跟踪数据
MyPath = App.Path
MyFile = MyPath & "\" & MyTextFile
i=0
filenum = FreeFile
Open MyFile For Input As filenum
do while Not EOF(filenum) Then '这里只读入第一行
i=i+1
Dim MyData As String
Line Input #filenum, MyData
if i=你要的行 then
temp=MyData
exit do
end if
loop
Close filenum
dgz01 2002-06-29
  • 打赏
  • 举报
回复
如果要多次访问,还不如把TXT读入数据库中,然后就方便了.

★★★★★
打工好辛苦
★★★★★
钞票好难赚
★★★★★
编程好伤神
★★★★★
光阴好易混
★★★★★
junwhj 2002-06-29
  • 打赏
  • 举报
回复
这样做效率太低,你把每一行的数据都改成定长的,以后用随机访问的方式直接跳到位置:100*行数,然后再读取此行数据
xxlroad 2002-06-28
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim i As Long
Dim s As String
Open "C:\aa.txt" For Input As #1
i = 0
Do Until EOF(1)
i = i + 1
Line Input #1, s
If i = 9 Then '读第九行
Print s
Exit Do '避免浪费
End If
Loop
Close #1
End Sub
kadina 2002-06-28
  • 打赏
  • 举报
回复
dim i,j as integer 'j是你要读的行数。
dim k as string
open "file" for input as #1
for i=1 to j
input #1,k
next i
close #1
'此时k中就应该是第j行的
zyl910 2002-06-28
  • 打赏
  • 举报
回复
Dim TempFile As Long
Dim LoadBytes() As Byte
dim LineStr() as string

TempFile=FreeFile
Open 文件名 For Binary As #TempFile
Redim LoadBytes(1 To Lof(TempFile)) As Byte
Get #TempFile,,LoadBytes
Close TempFile

LineStr=Split(StrConv(LoadBytes,vbUniCode),vbCrlf)



第n行的数据=LineStr(n-1)
xxlroad 2002-06-28
  • 打赏
  • 举报
回复
是不是这样??
zyl910 2002-06-28
  • 打赏
  • 举报
回复
先全部读取到一String数组
在对那个数组进行操作


别以为VB会把所有的事情做好
那人们学边还有什么用?
还能赚到钱?!
xxlroad 2002-06-28
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim i As Long
Dim s As String
Open "C:\aa.txt" For Input As #1
i = 0
Do Until EOF(1)
i = i + 1
Line Input #1, s
If i = 9 Then '读第九行
Print s
End If
Loop
Close #1
End Sub

7,763

社区成员

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

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