Results 1 to 7 of 7

Thread: write a new file?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    write a new file?

    code below use to extract data from file(data.txt).now,i need to write the value strLatitude,strLongitude,strDepthto a new file and save it. How to write the code?

    VB Code:
    1. private Sub readFile()
    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. Open strfilename For Input As #2
    14.     blnStartReading1 = False: blnStartReading2 = False
    15.    
    16.     While Not EOF(2)
    17.         Line Input #2, strTestData
    18.         If strTestData = "STOP" Then blnStartReading2 = False
    19.        
    20.         If blnStartReading1 = True And blnStartReading2 = True Then
    21.             A = Len(strTestData)
    22.             For B = 1 To A
    23.                 If Mid(strTestData, B, 1) = " " Then
    24.                     strLongitude = Mid(strTestData, 1, B - 1)
    25.                     strLatitude = Mid(strTestData, B + 1, A)
    26.                     Exit For
    27.                 End If
    28.             Next B
    29.         MsgBox " " & [COLOR=Magenta]strLatitude[/COLOR] & " " & [COLOR=Magenta]strLongitude[/COLOR]  & " " & [COLOR=Magenta]strDepth[/COLOR] & ""
    30.        End If
    31.                      
    32.         If blnStartReading1 = True And blnStartReading2 = False Then
    33.             i = InStr(strTestData, "174=")
    34.             ii = InStr(i + 1, strTestData, ";")
    35.                 If i < ii And i <> 0 Then
    36.                     strDepth = Mid(strTestData, i, ii - i)
    37.                 End If
    38.             blnStartReading2 = True
    39.         End If
    40.      
    41.       If strTestData = "EndHeader" Then blnStartReading1 = True
    42.       If strTestData = "STOP" Then blnStartReading2 = False
    43.      
    44.     Wend
    45. Close #2
    46. End Sub
    Attached Files Attached Files
    Last edited by junlo; Feb 6th, 2007 at 12:30 PM.

  2. #2
    New Member aStrat's Avatar
    Join Date
    Aug 2006
    Posts
    14

    Re: write a new file?

    do you need to write to the file as you are opening it throughout the loop, or do you just need to save the final value that strLat, strLong and strDepth has?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: write a new file?

    Quote Originally Posted by aStrat
    do you need to write to the file as you are opening it throughout the loop, or do you just need to save the final value that strLat, strLong and strDepth has?
    i need to write from opening it throughout the loop.

  4. #4
    New Member aStrat's Avatar
    Join Date
    Aug 2006
    Posts
    14

    Re: write a new file?

    I think this is what you are looking for, if not please let me know
    What I added is in Red.

    VB Code:
    1. Private Sub readFile()
    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. Open strFilename For Input As #2
    14.     blnStartReading1 = False: blnStartReading2 = False
    15.    
    16.     While Not EOF(2)
    17.         Line Input #2, strTestData
    18.         If strTestData = "STOP" Then blnStartReading2 = False
    19.        
    20.         If blnStartReading1 = True And blnStartReading2 = True Then
    21.             A = Len(strTestData)
    22.             For B = 1 To A
    23.                 If Mid(strTestData, B, 1) = " " Then
    24.                     strLongitude = Mid(strTestData, 1, B - 1)
    25.                     strLatitude = Mid(strTestData, B + 1, A)
    26.                     Exit For
    27.                 End If
    28.             Next B
    29.         MsgBox " " & strLatitude & " " & strLongitude & " " & strDepth & ""
    30.         [COLOR=Red]Open App.Path & "/NewFile.txt" For Append As #1
    31.             Print #1, " " & strLatitude & " " & strLongitude & " " & strDepth & ""
    32.         Close #1[/COLOR]
    33.        End If
    34.                      
    35.         If blnStartReading1 = True And blnStartReading2 = False Then
    36.             i = InStr(strTestData, "174=")
    37.             ii = InStr(i + 1, strTestData, ";")
    38.                 If i < ii And i <> 0 Then
    39.                     strDepth = Mid(strTestData, i, ii - i)
    40.                 End If
    41.             blnStartReading2 = True
    42.         End If
    43.      
    44.       If strTestData = "EndHeader" Then blnStartReading1 = True
    45.       If strTestData = "STOP" Then blnStartReading2 = False
    46.      
    47.     Wend
    48. Close #2
    49.  
    50. End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: write a new file?

    thanks aStrat. it is what i want. But now,i hv another problem.below is some of my original file.

    Header
    2.85667000
    99.70000000
    4.01666940
    101.46667000
    2
    Decimal
    1.01.33
    Some description
    EndHeader
    START 43, L, 174=0;133=300000
    100.81888000 3.85663000
    100.81778000 3.85625000
    100.80883000 3.85519000
    STOP
    START 43, L, 174=20;133=300000
    101.29401000 3.20013000
    101.29359000 3.20232000
    101.29134000 3.20748000

    STOP
    START 302, A, 18=1
    99.98843120 2.85667000
    99.70000000 2.85667000
    99.70000000 3.19926000

    STOP

    i only want to write the value(the blue one),don't need the red colour value.
    100.81888000 3.85663000 174=0
    100.81778000 3.85625000 174=0
    100.80883000 3.85519000 174=0
    101.29401000 3.20013000 174=20
    101.29359000 3.20232000 174=20
    101.29134000 3.20748000 174=20

    99.98843120 2.85667000 174=20
    99.70000000 2.85667000 174=20
    99.70000000 3.19926000 174=20

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: write a new file?

    You have to tell us why you don't want those values before we can help you. Is it that you don't want anything with the first number less than 100?
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: write a new file?

    Quote Originally Posted by Al42
    You have to tell us why you don't want those values before we can help you. Is it that you don't want anything with the first number less than 100?
    i hv a set of data which open with
    START ......
    100.81888000 3.85663000
    ......
    STOP

    i just need the value inside the
    START 43, L, 174=0;133=300000
    100.81888000 3.85663000
    100.81778000 3.85625000
    100.80883000 3.85519000
    STOP

    this means when have the 174=XXX after the START......, then i want to change it to format like this
    100.81888000 3.85663000 174=0
    100.81778000 3.85625000 174=0
    100.80883000 3.85519000 174=0

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