下面是一个示例的 VB.NET 代码,可以实现打开 SUM 文件,读取数据,并按要求判断格式并存储数据:

Imports System.IO

Module Module1
    Sub Main()
        Dim filePath As String = 'C:\path\to\your\file.sum' ' 替换为你的 SUM 文件路径
        Dim dataArray(,) As Integer

        ' 打开文件并读取数据
        Dim fileReader As StreamReader = New StreamReader(filePath)
        Dim line As String = fileReader.ReadLine()

        ' 判断第一行数据是否为 HINTCAD5.83_PM_SHUJU_PM
        If line = 'HINTCAD5.83_PM_SHUJU_PM' Then
            ' 从第4行开始,每间隔3行读取7个数据,存入一个二维数组,最后一行为0 0 0 0 0 0时停止
            Dim rowCount As Integer = 0
            Dim columnCount As Integer = 7
            dataArray = New Integer(,) {}
            While Not fileReader.EndOfStream
                line = fileReader.ReadLine()
                rowCount += 1
                If rowCount >= 4 AndAlso rowCount Mod 3 = 1 Then
                    Dim dataLine() As String = line.Split(' 'c)
                    If dataLine.Length = columnCount AndAlso dataLine.All(Function(x) IsNumeric(x)) Then
                        ReDim Preserve dataArray(rowCount \ 3, columnCount - 1)
                        For i As Integer = 0 To columnCount - 1
                            dataArray(rowCount \ 3 - 1, i) = Integer.Parse(dataLine(i))
                        Next
                    Else
                        Console.WriteLine('文件格式不正确')
                        Exit While
                    End If
                End If

                Dim lastLineData() As String = line.Split(' 'c)
                If lastLineData.Length = columnCount AndAlso lastLineData.All(Function(x) x = '0') Then
                    Exit While
                End If
            End While
        Else
            Console.WriteLine('文件格式不正确')
        End If

        fileReader.Close()

        ' 输出读取的数据
        For i As Integer = 0 To dataArray.GetLength(0) - 1
            For j As Integer = 0 To dataArray.GetLength(1) - 1
                Console.Write(dataArray(i, j) & ' ')
            Next
            Console.WriteLine()
        Next

        Console.ReadLine()
    End Sub
End Module

请将代码中的文件路径 'C:\path\to\your\file.sum' 替换为你的 SUM 文件的实际路径。代码会读取文件的第一行,判断是否为指定的字符串,如果是,则按要求读取数据到二维数组 dataArray 中,如果不是,则提示文件格式不正确。

读取的数据会以行列形式输出到控制台,你可以根据需要进行进一步处理。


原文地址: https://www.cveoy.top/t/topic/pIA4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录