Read One byte at a time

Posted on 2006-04-19
Last Modified: 2010-05-01
I want to analyze a file one character at a time, one byte at a time. What is the most efficient way of doing this? Should I read it from the drive one byte at a time, or read a big chunk and then loop through the chunk one byte at a time? And if reading a big chunk is the best way, what the most efficient way of looping through the chunk one byte at a time?

Question by:rcherne1
    1 Comment
    LVL 8

    Accepted Solution

    If you wanted to read one byte at a time this is efficient:

    Public Sub ReadFileByByte(ByVal FileName As String)
        Dim lngFileNum As Long
        Dim lngByteNum As Long
        Dim lngFileSize As Long
        Dim bytData As Byte

        'get next available file number
        lngFileNum = FreeFile()
        'open the file
        Open FileName For Binary As #lngFileNum
        'Get the file size
        lngFileSize = LOF(lngFileNum)

        'loop throu each byte of the file
        For lngByteNum = 1 to lngFileSize
            'read the bye at position lngByteNum
            Get #lngFileNum, lngByteNum, bytData

            'do something with the byte

        Next 'lngByteNum
        'close the file
        Close #lngFileNum
    End Sub



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now