VB.NET读取PM文件并提取数据
VB.NET读取PM文件并提取数据
本文将介绍如何使用VB.NET读取PM文件,判断文件格式是否符合规范,从第4行开始提取每行前7个数据并存入数组,如果遇到所有数据均为0的行则停止读取,并将读取的数据写入到另一个文本文件。以下是一个示例代码:
Imports System.IO
Module Module1
Sub Main()
' 读取文件
Dim filePath As String = "path/to/your/file.pm"
Dim lines As String() = File.ReadAllLines(filePath)
' 检查文件格式
If lines.Length = 0 OrElse lines(0) <> "HINTCAD5.83_PM_SHUJU_PM" Then
Console.WriteLine("文件格式不正确")
Return
End If
' 读取数据
Dim data As New List(Of Integer)()
For i As Integer = 3 To lines.Length - 1
Dim line As String = lines(i)
Dim values As String() = line.Split(" "c, StringSplitOptions.RemoveEmptyEntries)
For Each value As String In values
Dim intValue As Integer
If Integer.TryParse(value, intValue) Then
data.Add(intValue)
End If
Next
' 判断是否所有数据都为0
If data.All(Function(x) x = 0) Then
Exit For
End If
Next
' 写入数据到文件
Dim outputFilePath As String = "path/to/your/output/file.txt"
File.WriteAllLines(outputFilePath, data.Select(Function(x) x.ToString()))
End Sub
End Module
请将代码中的 "path/to/your/file.pm" 替换为您实际的PM文件路径,并将 "path/to/your/output/file.txt" 替换为您想要将读取的数据写入的输出文件路径。
原文地址: https://www.cveoy.top/t/topic/pMCt 著作权归作者所有。请勿转载和采集!