Link to home
Start Free TrialLog in
Avatar of jitganguly
jitganguly

asked on

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 ?
Avatar of bramsquad
bramsquad
Flag of United States of America image

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
Avatar of Rick_Townsend
Rick_Townsend

> 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
Plus, close your files:

Close inFileNo
Avatar of jitganguly

ASKER

Let me test and will get back soon
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
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
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?

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
"

Avatar of Mike McCracken
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
>>Should be an AND
Coming out from the loop for the very first read

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


ASKER CERTIFIED SOLUTION
Avatar of bramsquad
bramsquad
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks like it is working, I need to get it verified by my users will let you know and award you 'A' tomorrow

Thanks
It worked. Thanks