How to read/write to a file contains of many different symbols...

Hi!

Have a binary file, that contains of many different characters...
My problem here is that it dosent read the hole file, why ???

Read a file with this Sourcecode:
************************
Dim strIn As String
Dim IngInFile
Dim s1
Dim countli
Dim ferdig As Boolean

   lngInFile = FreeFile
   
   s1 = App.Path & "\test1.xdv"
   Open s1 For Input As lngInFile
     
   WhileNot EOF(lngInFile)
     Line Input #lngInFile, strIn
     If InStr(1, strIn, "Ã") Then
       countli=countli + 1
     endif
     Debug.Print strIn
   Wend
   
   Close lngInFile

**********************************

So, why does the sub exit before its finish ????

Please help ASAP.

Thanks

Tor


LVL 2
team2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
Thats not the way to read a binary file. You are reading it line by line but the lines might not be terminated with a carriage return line feed when you are working with binary data. To read a binary file you would use something like this:

************************
Dim strIn As String
Dim IngInFile
Dim s1
Dim countli
Dim ferdig As Boolean

   lngInFile = FreeFile
   
   s1 = App.Path & "\test1.xdv"
   Open s1 For Binary Access Read As #lngInFile
     
    strIn = Space$(LOF(s1))
   
    Get #lngInFile, , strIn

   Close #lngInFile

**********************************
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try reading the whole thing in at once and then use Split():

    Dim fileName As String
    fileName = App.Path & "\test1.xdv"
    Open fileName For Binary Access Read As #1
    entireFile = Input(LOF(1), 1)
    Close #1
   
    Dim lines As Variant
    lines = Split(entireFile, vbCrLf)
   
    Dim countli
    Dim i As Integer
    For i = 0 To UBound(lines)
        If InStr(lines(i), "Ã") > 0 Then
            countli = countli + 1
            Debug.Print "Found in: " & lines(i)
        End If
    Next i
    Debug.Print "countli = " & countli
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I forgot to declare "entireFile" in there:
 
    Dim entireFile As String
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Welcome back vinnyd79...   =)
0
team2005Author Commented:
Hi!

Tryed your code vinnyd79
*******************

Dim strIn As String
Dim IngInFile
Dim s1
Dim countli
Dim ferdig As Boolean

   lngInFile = FreeFile
   
   s1 = App.Path & "\test1.xdv"
   Open s1 For Binary Access Read As #lngInFile
     
    strIn = Space$(LOF(s1))
   
    Get #lngInFile, , strIn

   Close #lngInFile

************************

But get an error on the line ---> strIn = Space$(LOF(s1))  ????

What does the Space.... do ???

Thanks for helping me out here.

Tor
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you try my code?

To fix vinnyd79s code, the line should be:

    strIn = Space$(LOF(lngInFile))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
team2005Author Commented:
Hi!

The source code from vinny... working now.
Thanks to you idle_mind

So i think i split the points....

THANKS

Cheers,
Tor


0
Mike TomlinsonMiddle School Assistant TeacherCommented:
In answer to your question, the Space() function creates a string filled with spaces.  The parameter you pass in tells it how many spaces to use.

The whole file is then read in because the size of the variable in the Get statement matches the size of the file:

    Get #lngInFile, , strIn

In my example, I used a different approach:

    Open fileName For Binary Access Read As #1
    entireFile = Input(LOF(1), 1)
    Close #1

Here we read in the whole file because we tell it to return XXX number bytes in the first parameter to Input(), and the LOF() function returns the size of the file.
0
vinnyd79Commented:
Thanks Idle Mind. Don't know what I was thinking putting the filename in instead of the Open File Number.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
=)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.