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: 206
  • Last Modified:

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


0
team2005
Asked:
team2005
  • 6
  • 2
  • 2
2 Solutions
 
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
Industry Leaders: 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!

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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