VB6.0中如何实现逐行读入文本文件?

VB6.0中如何实现逐行读入文本文件?,第1张

VB6.0可以用一次读取文本文件全部文本内容,然后使用Split函数来实现一行一行提取文本框行内容。

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

以下代码是可以提取文本文件任何一行内容的:

Private Sub Command1_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)

    RichTextBox1 = strWj

    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

看了代码,好像:

数组lonposition中存储着行数,是int类型

你要将lonposition指定行的内容,赋予tmp,但是最后打印的Str是从何而来?

以下是一次循环代码:

注意:

1.假设读取的文件是ansi编码

2.lonposition中存储着行数,是int类型 Dim FSO, waita, osh

Set FSO = CreateObject("Scripting.FileSystemObject")

Set osh = CreateObject("WScript.Shell")

myfile = "C:\Users\DELL\Desktop\11072503.000"

If FSO.FileExists(myfile) Then

    'should replace string:readfile1 ,<format> ,True

    Set readfile1 = FSO.OpenTextFile(myfile, 1, True, 0)

    myallfiletemp = readfile1.ReadAll

    readfile1.Close

    myallfile_arr = Split(myallfiletemp, vbCrLf)

    For Each t In LonPosition

        'fcc = Split(t, ",")(0)

        Str = myallfile_arr(CInt(t))

        Print Str

    Next

End If


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12079165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存