Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

Reading text file

This is my file

{4:
:20:LM083119ZUDD1  
:21:OB04.02097      
:32A:040617USD1962,94
:50:XXXXXXXX XXXXXXXXX XXXXXXX
CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
:53D:/000000005003
XXXXXXXX XXXXXXXXX XXXXXXX
CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
:58D:/000000005258
XXXXXXXX XXXXXXXXX XXXX XXXX
TWO EXCHANGE SQUARE 39TH FLOOR
8 CONNAUGHT PLACE
CENTRAL HONG KONG
:90C:002
:61:0406170615D X1962,94XXXX0000000000005003   00000
:61:0406170615C X1962,94XXXX0000000000005258   00000
-}$

I need to read this file  as
condition #1 >> if the first 3 characters are :50 then put that record to the first element to an array
condition #2 >> Read the following records until a line begining with : is encountered and store it in next element

so condition #1 >> would match :50:XXXXXXXX XXXXXXXXX XXXXXXX so it would be myarray(1)= "condition #1 "

condition #2 >> would match
CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
so it would be myarray(2)= "condition #2 "


How do I do this ?
0
jitganguly
Asked:
jitganguly
  • 6
  • 4
  • 2
  • +1
1 Solution
 
bramsquadCommented:
this should do the trick

Open "C:\your_file.txt" For Input As #1

Dim myLine As String
Dim arr() As String
Dim count As Integer

count = 1

While Not EOF(1)
    'get a new line
    Line Input #1, myLine

    If Mid$(myLine, 1, 3) Like ":50" Then
        arr(count) = "condition #1 "
        count = count + 1

        'get the rest of the values
        While ((Mid$(myLine, 1, 1) <> ":")  Or (Not EOF(1)))
            Line Input #1, myLine
        Wend

        arr(count) = "condition #2 "
        count = count + 1
    End If

Wend
0
 
Rick_TownsendCommented:
> While ((Mid$(myLine, 1, 1) <> ":")  Or (Not EOF(1)))

That should be   AND (Not EOF(1)))
shouldn't it?

Also, it's simple and much safer to get the file number from the FreeFile function.

eg:
inFileNo = FreeFile
Open "C:\your_file.txt" For Input As inFileNo
...
Line Input #inFileNo, myLine
0
 
Rick_TownsendCommented:
Plus, close your files:

Close inFileNo
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jitgangulyAuthor Commented:
Let me test and will get back soon
0
 
bramsquadCommented:
not sure about the freefile function

but i think it should be OR and not AND....

im saying "get the lines until you come across a colon, OR until you come across the end of the file"

if you had an AND there, it might be an infinite loop, becuase the file may not have a colon at the end of the file.

~b
0
 
jitgangulyAuthor Commented:
Sorry does not work

Open strNextFile For Input As #2
        While Not EOF(2)
           
            Line Input #2, strNextLine
            If Mid$(strNextLine, 1, 3) Like ":50" Then
                strParty(1) = strNextLine
                i = i + 1
             MsgBox strNextLine
                While ((Mid$(strNextLine, 1, 1) <> ":") Or (Not EOF(2)))
                    Line Input #2, strNextLine
                Wend
                i = i + 1
                MsgBox strNextLine
            End If
        Wend


The first  strNextLine should produce me :50:XXXXXXXX XXXXXXXXX XXXXXXX and second one
CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
0
 
bramsquadCommented:
what do you want to hold in your array?

the information?  if so, strNextLine will not get that whole address becuase it is on three different lines, so it will get it one at a time.

how do you want to store this information?

0
 
jitgangulyAuthor Commented:
Lets forget about array for the time being

Here is my code
 Open strNextFile For Input As #2
        While Not EOF(2)
           
            Line Input #2, strNextLine
            If Mid$(strNextLine, 1, 3) Like ":50" Then
                strParty(1) = strNextLine
                i = i + 1
            MsgBox strNextLine
              While ((Mid$(strNextLine, 1, 1) <> ":") Or (Not EOF(2)))
              'If (EOF(2) Or Mid$(strNextLine, 1, 1) = ":") Then Exit Do
                     Line Input #2, strNextLine
                    jit = jit & strNextLine
              Wend
                i = i + 1
                MsgBox jit
            End If
        Wend

MsgBox strNextLine should be ":50:XXXXXXXX XXXXXXXXX XXXXXXX "

MsgBox jit should be

"CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
"

0
 
mlmccCommented:
Should be an AND

>> im saying "get the lines until you come across a colon, OR until you come across the end of the file
However you are testing
    While ((Mid$(myLine, 1, 1) <> ":")  Or (Not EOF(1)))

A  OR   B  is equivalent to ~A AND ~B

mlmcc
0
 
jitgangulyAuthor Commented:
>>Should be an AND
Coming out from the loop for the very first read

While ((Mid$(strNextLine, 1, 1) <> ":") And (Not EOF(2)))


0
 
bramsquadCommented:
my bad, i was wrong...

either way, here is a working program which will output what you are looking for

Private Sub btnStep5_Click()
        Open "D:\test.txt" For Input As #2
        Dim strNextLine As String
        Dim strParty(1) As String
        Dim test As String
        Dim i As Integer
       
        While Not EOF(2)
           
            Line Input #2, strNextLine
            If Mid$(strNextLine, 1, 3) Like ":50" Then
                strParty(i) = strNextLine
                i = i + 1
             MsgBox strNextLine
             test = ""
             Line Input #2, strNextLine
                While ((Mid$(strNextLine, 1, 1) <> ":") And (Not EOF(2)))
                    test = test + strNextLine + vbCrLf
                    Line Input #2, strNextLine
                Wend
                i = i + 1
                MsgBox test
            End If
        Wend
        Close #2
End Sub
0
 
jitgangulyAuthor Commented:
Looks like it is working, I need to get it verified by my users will let you know and award you 'A' tomorrow

Thanks
0
 
jitgangulyAuthor Commented:
It worked. Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now