Results 1 to 4 of 4

Thread: help to change code??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Unhappy help to change code??

    VB Code:
    1. Private Sub Form_Load()
    2. Dim blnStartReading1 As Boolean, blnStartReading2 As Boolean
    3. Dim strTestData As String
    4. Dim A As Integer
    5. Dim B As Integer
    6. Dim i As Integer
    7. Dim ii As Integer
    8. Dim strLatitude As String
    9. Dim strLongitude As String
    10. Dim strDepth As String
    11. ChDir App.Path
    12.  
    13.  
    14. 'Open strfilename For Input As #2
    15. Open "test.txt" For Input As #2
    16.     blnStartReading1 = False: blnStartReading2 = False
    17.    
    18.     While Not EOF(2)
    19.         Line Input #2, strTestData
    20.         If strTestData = "STOP" Then blnStartReading2 = False
    21.        
    22.         If blnStartReading1 = True And blnStartReading2 = True Then
    23.             A = Len(strTestData)
    24.             For B = 1 To A
    25.                 If Mid(strTestData, B, 1) = " " Then
    26.                     strLongitude = Mid(strTestData, 1, B - 1)
    27.                     strLatitude = Mid(strTestData, B + 1, A)
    28.                     Exit For
    29.                 End If
    30.             Next B
    31.            
    32.         'MsgBox " " & strLatitude & " " & strLongitude & " " & strDepth & ""
    33.         Open App.Path & "/Data.txt" For Append As #3
    34.             Print #3, " " & strLatitude & " " & strLongitude & " " & strDepth & ""
    35.         Close #3
    36.  
    37.        End If
    38.                      
    39.         If blnStartReading1 = True And blnStartReading2 = False Then
    40.             i = InStr(strTestData, "174=")
    41.             ii = InStr(i + 1, strTestData, ";")
    42.                 If i < ii And i <> 0 Then
    43.                     strDepth = Mid(strTestData, i, ii - i)
    44.                 End If
    45.             blnStartReading2 = True
    46.         End If
    47.      
    48.       If strTestData = "EndHeader" Then blnStartReading1 = True
    49.       If strTestData = "STOP" Then blnStartReading2 = False
    50.      
    51.     Wend
    52. Close #2
    53. End Sub

    From the "text.txt" file,i only need the value inside "START 43, L, 174=0;133=300000" and "STOP" to change to the format below. By using the code above,i found mistake was happen(the blue color). i don't need to save the blue color value.
    3.85663000 100.81888000 174=0
    3.85625000 100.81778000 174=0
    3.85519000 100.80883000 174=0
    3.20013000 101.29401000 174=20
    3.20232000 101.29359000 174=20
    3.20748000 101.29134000 174=20
    2.85667000 99.98843120 174=20
    2.85667000 99.70000000 174=20
    3.19926000 99.70000000 174=20
    3.73802000 100.94623000 174=20
    3.73897000 100.94658000 174=20
    3.74430000 100.94950000 174=20
    3.76365000 100.98286000 174=20
    Attached Files Attached Files
    Last edited by junlo; Feb 13th, 2007 at 05:12 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: help to change code??

    this code will open your file and read it all into memory to work on
    VB Code:
    1. Dim strfile As String, resarr() As String, i As Long, pos as long, pos1 as long
    2. Open "test.txt" For Input As 1
    3.     strfile = Input(LOF(1), #1) ' read whole file
    4.     Close 1
    5.     pos = 1
    6.     Open "Data.txt" For Append As 1
    7.     Do
    8.     pos = InStr(pos + 1, strfile, "START 43, L, 174=") 'find start position
    9.     If pos = 0 Then Exit Do ' if no start position found finish
    10.    
    11.     pos1 = InStr(pos + 1, strfile, vbNewLine) + 2
    12.     resarr = Split(Mid(strfile, pos1, InStr(pos1, strfile, "STOP") - pos1 - 2), vbNewLine)
    13.       ' split the text in the lines between start and stop into an array
    14.     For i = 0 To UBound(resarr)  ' process each line in array
    15.         pos1 = InStr(resarr(i), " ") + 1
    16.         resarr(i) = Mid(resarr(i), pos1) & " " & Left(resarr(i), pos1 - 2) _ 'swap order of line
    17.         & " " & Mid(strfile, pos + 13, InStr(pos + 14, strfile, ";") - pos - 13) ' add the 174 bit
    18.     Next
    19.     Print #1, Join(resarr, vbNewLine) ' print all lines in group to file
    20.     Loop
    21.     Close 1
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: help to change code??

    thanks westconn1.

    but i still got problem, because my original file hv other format like below.It is not only format in blue color

    START 43, L, 174=0;133=300000
    3.85663000 100.81888000
    .....
    STOP
    START 43, L, 133=250000;174=5
    ......
    STOP
    START 43, L, 133=250000;174=50;102="Approximate depth contour"
    .....
    STOP
    START 43, L, 174=50
    ......
    STOP
    START 43, L, 174=100;133=300000
    .....
    STOP

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: help to change code??

    change the initial search string to find the start position so that it will work with all your strings,

    i thought you only wanted to find the ones that had 174 = something so i searched upto the 174, if you want all values, just search to the "L"

    if you need more help, give more examples of ones you want to include and ones you want to exclude, i only worked to the sample file you posted
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width