Home > Software engineering >  Want to write a VB module line by line read a TXT file and will split it into the content of the two
Want to write a VB module line by line read a TXT file and will split it into the content of the two

Time:09-28

The code below is my own definition of a function, also can't use, don't know where I went wrong, I want to call in the process of direct writing Pipedat2D (1, 2) can read the value of the corresponding array
The Public Function Pipedat2D (ByVal I %, j %)
Open the App. The Path & amp; ", def, piping configuration. TXT "For Input As # 1
I=1
Do Until EOF (1)
Input # 1, a
T=Split (a, "")
For j=0 To 4
Pipedat2D (I, j)=t (j)
Next
I=I + 1
Loop
Close # 1
End the Function
Below is my the contents of a text file:
BG bilge water piping rubber gasket galvanized 0.3
CL cooling water piping rubber gasket galvanized 0.45
CA compressed air pipe rubber gasket phosphating 1.0
LO, lub oil pipeline no asbestos fiber pickling 0.3
FF fire piping rubber gasket galvanized 0.6
FT fuel delivery pipe pickling 0.4 no asbestos fiber

CodePudding user response:

Ace to help me answer the genuflect is begged ah, thank you very much!

CodePudding user response:

If you just to read text directly the value of the corresponding ranks as follows (seems you line is based on one count, column based on zero count) :

 Public Function Pipedat2D (ByVal I As String, ByVal j As String) 
Dim strRow As String, strCol () As String, n As Long

Open the App. The Path & amp; ", def, piping configuration. TXT "For Input As # 1
N=1
Do Until EOF (1)
The Line Input # 1, strRow
If n=I Then
StrCol=Split (strRow, "")
If j & lt;=UBound (strCol) Then
Pipedat2D=strCol (j)
End the If
Close # 1
The Exit Function
End the If
N=n + 1
Loop
Close # 1
End the Function

CodePudding user response:

If TXT text longer, each reading efficiency is low, can also query for the first time to add the real array:

 Public strPipedata2D () As String 
Public Populated As Boolean

Public Function Get_Pipedat2D (ByVal I As Long, ByVal j) As Long As the String
If Not Populated Then
Init_Pipedata_Array
End the If

If I & lt;=UBound (strPipedata2D, 1) And j & lt; Then=UBound (strPipedata2D, 2)
Get_Pipedat2D=strPipedata2D (I, j)
End the If
End the Function

Public Sub Init_Pipedata_Array ()
Dim r As Long, c As Long, I As Long, j As Long
Dim strRow As String, strCol () As String
Dim strTmp () As String, OK As Boolean

The Open c: \ "def \ 1 u? Mu?????? .txt "For Input As # 1

Do Until EOF (1)
The Line Input # 1, strRow
StrCol=Split (strRow, "")
If r=0 Then
C=UBound (strCol)
ElseIf c & gt; UBound (strCol) Then
MsgBox "Bad file!"
The Exit Sub
End the If

ReDim Preserve strTmp (c, r)
For j=0 To c
StrTmp (j, r)=strCol (j)
Next j
R=r + 1
Loop
OK=True
Close # 1

If Not OK Then Exit Sub
ReDim strPipedata2D (1 To r, c)
For I=1 To r
For j=0 To c
StrPipedata2D (I, j)=strTmp (j, I - 1)

The Debug. Print strPipedata2D (I, j) & amp; "";
Next j
The Debug. Print "
"Next I
Populated=True
End Sub
in the same way, the base is 1 as a starting point,

CodePudding user response:

 
Option Explicit
Dim iData ()

Private Sub Form_Load ()
The Static n As Integer
"C: \ 1. TXT" Open For Input As # 1
Do Until EOF (1)
ReDim Preserve iData (n)
The Line Input # 1, iData (n)
N=n + 1
Loop
Close # 1
End Sub

The Public Function Pipedat2D (ByVal nRow As Long, ByVal nCol As Long) As String
Pipedat2D=Split (iData (nRow - 1), "") (nCol - 1)
End the Function

Private Sub Command1_Click ()
MsgBox Pipedat2D (3, 1)
End Sub
  • Related