注册 登录
编程论坛 VB.NET论坛

开档案写档案

yz1025 发布于 2024-05-16 15:41, 435 次点击
如何开档案逐行读取内容和
逐行写入阵列资料

发现书上的写法跟VB6差很多,用了AI给的代码好像不能用
若能给个简单范例就好了

[此贴子已经被作者于2024-5-16 15:45编辑过]

7 回复
#2
yiyanxiyin2024-05-16 15:48
什么档案?一般txt文件还是word文档还是其他的?阵列资料又是什么?如果有明确的数据格式,给出格式
#3
yz10252024-05-16 15:49
程序代码:

Private Sub GetRegDataInStruct()
Dim FileNum As Integer, i As Integer, bStart As Boolean
Dim TempString As String
   
    With RegData(0)
   
        For i = 0 To .RegFileCount - 1
        
            If i = 0 Then .RegTitleCount = 0
            
            FileNum = FreeFile: bStart = False
            Open .RegFile(i).RegFileName For Input As #FileNum
                Do While Not EOF(FileNum)
                    
                    Line Input #FileNum, TempString
                    
                    If ((bStart = True) And (TempString <> "")) Then
                        ReDim Preserve .RegFile(i).RegFileData(.RegFile(i).RegFileDataCount)
                        .RegFile(i).RegFileData(.RegFile(i).RegFileDataCount) = TempString
                        .RegFile(i).RegFileDataCount = .RegFile(i).RegFileDataCount + 1
                    End If
                    
                    If ((i = 0) And (bStart = False)) Then
                        ReDim Preserve .RegTitle(.RegTitleCount)
                        .RegTitle(.RegTitleCount) = TempString
                        .RegTitleCount = .RegTitleCount + 1
                    End If
                    
                    If InStr(UCase$(TempString), UCase$(KeyWord)) <> 0 Then
                        bStart = True
                    End If
                    
                    DoEvents
                Loop
            Close #FileNum
        Next i
        
        For i = 0 To .RegFileCount - 1
            If GetNum(.RegFile(i).RegFileData(.RegFile(i).RegFileDataCount - 1)) > .MaxContactNum Then
               .MaxContactNum = GetNum(.RegFile(i).RegFileData(.RegFile(i).RegFileDataCount - 1)) '最大Contact Number
            End If
        Next i
        
    End With

End Sub


想把这段改为语法
#4
yz10252024-05-16 15:50
程序代码:

Private Sub OutputBuffData()
Dim FileNum As Integer, i As Long, j As Long, TotalCountNum As String
Dim FileN As String, MyTime As String, Temp() As String

On Error GoTo ErrorHandling

    With RegData(0)
   
        MyTime = .TimeFlowNum
        
        FileN = .RegFilePath & .m_LotID & "_" & .m_WaferID & "_" & MyTime & Extension
        
        FileNum = FreeFile
        Open FileN For Output As #FileNum
        
            For i = 0 To .RegTitleCount - 1
                Print #FileNum, .RegTitle(i)
            Next i
            
            TotalCountNum = 0
            For i = 0 To RegData(0).MaxContactNum - 1
                For j = 0 To 7 'All Site --> 0 ~ 7
                    If Trim(RegData(0).AllRegData(i, j)) <> "" Then
                       TotalCountNum = TotalCountNum + 1
                       Print #FileNum, CStr(TotalCountNum) & " " & RegData(0).AllRegData(i, j)
                    End If
                Next j
            Next i
        Close #FileNum
        
        'debug log
        FileN = "C:\MiniRegMergeTool_" & Format(Now, "yyyymm") & ".log"
        FileNum = FreeFile
        Open FileN For Append As #FileNum
            Print #FileNum, Format(Now, "yyyy-mm-dd hh:mm:ss") & " -> " & TotalCountNum & vbCrLf
        Close #FileNum
        
    End With
   
    'Erase RegData, NewRegData
   
Exit Sub

ErrorHandling:
    Resume Next
End Sub
#5
yz10252024-05-16 15:52
因为VB6二维阵列开不到6万以上,需求至少要到12万,所以改成.NET试试看
#6
yz10252024-05-16 17:05
还是爆

程序代码:

Private Sub GetRegDataInStruct()
        Dim i As Integer, bStart As Boolean

        With RegData(0)

            For i = 0 To .RegFileCount - 1

                If i = 0 Then .RegTitleCount = 0
                Dim sr As StreamReader = New StreamReader(.RegFile(i).RegFileName)
                Dim line As String

                Do
                    line = sr.ReadLine()
                    'Console.WriteLine(line)
                    If ((bStart = True) And (line <> "")) Then
                        ReDim Preserve .RegFile(i).RegFileData(.RegFile(i).RegFileDataCount)
                        .RegFile(i).RegFileData(.RegFile(i).RegFileDataCount) = line
                        .RegFile(i).RegFileDataCount = .RegFile(i).RegFileDataCount + 1
                    End If

                    If ((i = 0) And (bStart = False)) Then
                        ReDim Preserve .RegTitle(.RegTitleCount)
                        .RegTitle(.RegTitleCount) = line
                        .RegTitleCount = .RegTitleCount + 1
                    End If

                    If InStr(UCase$(line), UCase$(KeyWord)) <> 0 Then
                        bStart = True
                    End If

                Loop Until line Is Nothing
                sr.Close()

            Next i

            For i = 0 To .RegFileCount - 1
                If GetNum(.RegFile(i).RegFileData(.RegFile(i).RegFileDataCount - 1)) > .MaxContactNum Then
                    .MaxContactNum = GetNum(.RegFile(i).RegFileData(.RegFile(i).RegFileDataCount - 1)) '最大Contact Number
                End If
            Next i

        End With

    End Sub


程序代码:

Private Sub OutputBuffData()
        Dim FileNum As Integer, i As Long, j As Long, TotalCountNum As String
        Dim FileN As String, MyTime As String, Temp() As String

        On Error GoTo ErrorHandling

        With RegData(0)

            MyTime = .TimeFlowNum

            FileN = .RegFilePath & .m_LotID & "_" & .m_WaferID & "_" & MyTime & Extension

            Dim sw As StreamWriter = New StreamWriter(FileN)
            For i = 0 To .RegTitleCount - 1
                sw.WriteLine(.RegTitle(i))
            Next i

            TotalCountNum = 0
            For i = 0 To RegData(0).MaxContactNum - 1
                For j = 0 To 7 'All Site --> 0 ~ 7
                    If Trim(RegData(0).AllRegData(i, j)) <> "" Then
                        TotalCountNum = TotalCountNum + 1
                        sw.WriteLine(CStr(TotalCountNum) & " " & RegData(0).AllRegData(i, j))
                    End If
                Next j
            Next i
            sw.Close()

            'debug log
            '    FileN = "C:\MiniRegMergeTool_" & Format(Now, "yyyymm") & ".log"
            '    FileNum = FreeFile()
            'Open FileN For Append As #FileNum
            '    Print #FileNum, Format(Now, "yyyy-mm-dd hh:mm:ss") & " -> " & TotalCountNum & vbCrLf
            'Close #FileNum

        End With

        'Erase RegData, NewRegData

        Exit Sub

ErrorHandling:
        Resume Next
    End Sub
#7
yuma2024-05-20 18:49
回复 6楼 yz1025
你为什么没有写注释的习惯?

#8
yz10254 天前 16:37
以下是引用yuma在2024-5-20 18:49:38的发言:

你为什么没有写注释的习惯?


一来因为是我写的,架构自己清楚,写不写没差别
二来因为是我写的,没人会接,有问题没人会帮忙,一切还是得自己来

若复杂一点的部分还是会写注释,只是目前这只程式没复杂到那个地步
1