vb读取文本文件某行的内容

VB可以使用二进制方式打开文本文件,以Get 语句读到字节数组,再使用StrConv 函数将其转换为字符串,而后使用Split函数文本的字符串数组及总句数,使用InputBox 函数来输入需要读某行文本。

Get 语句
将一个已打开的磁盘文件读入一个变量之中。
StrConv 函数
返回按指定类型转换的 Variant (String)。
Split函数

返回一个下标从零开始的一维数组,它包含指定数目的子字符串。
InputBox 函数

在一对话框来中显示提示,等待用户输入正文或按下按钮,并返回包含文本框内容的 String。

以下是使用上述函数等的具体代码:

Private Sub Command3_Click()
    Dim strWj As String
    Dim strJ() As String
    Dim aryContent() As Byte
    Dim i As Long
    Dim j As Long
    CommonDialog1.CancelError = True ' 设置“CancelError”为 True
    On Error GoTo ErrHandler
    CommonDialog1.Flags = cdlOFNHideReadOnly    ' 设置标志
    ' 设置过滤器
    CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
    CommonDialog1.FilterIndex = 2    ' 指定缺省的过滤器
    CommonDialog1.ShowOpen    ' 显示“打开”对话框
    ' 显示选定文件的名字
    'MsgBox CommonDialog1.FileName
    Open CommonDialog1.FileName For Binary As #1
        ReDim aryContent(LOF(1) - 1)
        Get #1, , aryContent
    Close #1
    strWj = StrConv(aryContent, vbUnicode)
    Text1 = strWj
    strJ = Split(strWj, vbCrLf)
    i = UBound(strJ)
    Text2 = i + 1
    j = InputBox("输入需要显第几句", j)
    j = j - 1
    Label1.Caption = j + 1 & ":" & strJ(j)
    Exit Sub
ErrHandler:
    ' 用户按了“取消”按钮
    Exit Sub
End Sub

在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值