Link to home
Start Free TrialLog in
Avatar of team2005
team2005

asked on

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


SOLUTION
Avatar of vinnyd79
vinnyd79

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
Avatar of Mike Tomlinson
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
I forgot to declare "entireFile" in there:
 
    Dim entireFile As String
Welcome back vinnyd79...   =)
Avatar of team2005
team2005

ASKER

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
ASKER CERTIFIED SOLUTION
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
Hi!

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

So i think i split the points....

THANKS

Cheers,
Tor


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.
Thanks Idle Mind. Don't know what I was thinking putting the filename in instead of the Open File Number.