We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

team2005
team2005 asked
on
Medium Priority
245 Views
Last Modified: 2010-05-01
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


Comment
Watch Question

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
I forgot to declare "entireFile" in there:
 
    Dim entireFile As String
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Welcome back vinnyd79...   =)

Author

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
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Did you try my code?

To fix vinnyd79s code, the line should be:

    strIn = Space$(LOF(lngInFile))

Author

Commented:
Hi!

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

So i think i split the points....

THANKS

Cheers,
Tor


Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.

Commented:
Thanks Idle Mind. Don't know what I was thinking putting the filename in instead of the Open File Number.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
=)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.