VB如何指定读取文本文件的某一行

如题,要完整代码!谢谢... 如题,要完整代码!谢谢 展开
 我来答
xuyile510824
2007-10-26 · TA获得超过582个赞
知道小有建树答主
回答量:557
采纳率:0%
帮助的人:584万
展开全部
仅提供代码给你 代码并非我写(版权还是要声明的)
Private Sub Command1_Click()
lines "d:\txt.txt", 10, 1 '第10行
End Sub
Function lines(txtpath As String, ByVal startline As Integer, linenum As Integer) As String '显示 txtpath 文件的从startline 行开始的 linenum 行的内容
lines = ""
Dim filetxt As String, x As Variant, i As Integer
filetxt = String(FileLen(txtpath), " ")
Open txtpath For Binary As 1
Get #1, , filetxt
Close 1
x = Split(filetxt, vbCrLf)
msgbox ubound(x)+1 '行数
If startline > UBound(x) Then MsgBox "行溢出", 64, "err!": Exit Function
If startline <= UBound(x) Then
If startline + linenum <= UBound(x) Then
For i = startline To startline + linenum - 1
lines = lines & x(i) & " "
Next
Else
For i = startline To UBound(x)
lines = lines & x(i) & " "
Next
End If: End If

End Function
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
dl_会飞的青蛙
2007-10-26 · TA获得超过2456个赞
知道大有可为答主
回答量:3294
采纳率:0%
帮助的人:3365万
展开全部
'添加 Command1 Command2 Text1 Text2 Label1
'在Text2输入欲读取第几行的行号,再点击Command1
'Command2是倒数第二行的内容

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const EM_GETLINECOUNT = &HBA
Const EM_GETLINE = &HC4
Dim linecnt As Integer, i As Long, j As Long
Private Sub Form_Load()
Open "c:\test.txt" For Binary As #1
Text1.Text = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
linecnt = TextBoxLineCnt(Text1) '本文件总行数
Text2.Text = "1"
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Val(Text2.Text) > 0 and Val(Text2.Text) < linecnt Then
command1_click
End If
End If
End Sub

Private Sub Command1_Click()
Dim str1 As String
i = CInt(Text2.Text) - 1
str1 = getlinetext(Text1, i) '取得Text2中的指定行的内容
Label1.Caption = "第 " & Trim(Text2.Text) & " 行的内容是:" & vbCrLf & Chr(10) & str1
End Sub

Private Sub Command2_Click() '倒数第二行
Dim str1 As String
If linecnt >= 2 Then
j = linecnt - 2 '第一行是0,最后一行是Linecnt - 1
str1 = getlinetext(Text1, j)
Label1.Caption = "倒数第二行的内容是:" & vbCrLf & Chr(10) & str1
End If
End Sub

Public Function getlinetext(Text1 As TextBox, ByVal ntx As Long) As String
Dim str1(255) As Byte
Dim str2 As String
str1(0) = 255
i = SendMessage(Text1.hwnd, EM_GETLINE, ntx, str1(0))
If i = 0 Then
getlinetext = ""
Else
str2 = StrConv(str1, vbUnicode)
getlinetext = Left(str2, InStr(1, str2, Chr(0)) - 1)
End If
End Function

Public Function TextBoxLineCnt(ctl As TextBox) As Long
TextBoxLineCnt = SendMessage(ctl.hwnd, EM_GETLINECOUNT, 0, 0)
End Function

参考资料: vb吧

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
VB妮可
推荐于2016-05-22 · TA获得超过3333个赞
知道大有可为答主
回答量:2574
采纳率:0%
帮助的人:0
展开全部
Private Sub Command1_Click()
Dim n%, a

Open "c:\1.txt" For Binary As #1
a = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1

b = Split(a, vbCrLf)

n = Val(InputBox("你想读取第几行?"))

If n < 0 Or n > UBound(b) + 1 Then
MsgBox "行数有问题!!"
Exit Sub
End If

Print b(n - 1)
End Sub
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式