Read File Byte by Byte

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wquatan
    New Member
    • Oct 2007
    • 48

    Read File Byte by Byte

    Does someone know an (fast) example of reading files in VBA, Byte per Byte ?
    I need to be able to take action on every single byte

    Thx
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32663

    #2
    I think you're looking for Get#.

    FreeFile(), Open#, Put# & Close# are all related and I would recommend a thorough reading of the help topic before starting along these lines.

    It's possible to do, and it can work quite well too, but it's certainly not the default way of handling file I/O so don't expect it to be straightforward .

    I use it a bit, as it's perfect for some situations, but generally only when there's no better way of doing what I need.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by wquatan
      Does someone know an (fast) example of reading files in VBA, Byte per Byte ?
      I need to be able to take action on every single byte

      Thx
      The following code will Open a File (C:\Windows\Sys tem.ini), read its contents Byte-by-Byte, display each Character's Position, Decimal and Hexadecimal equivalents, and the actual Character itself. I posted a partial display of the Output for you to see, any questions, feel free to ask.
      Code:
      Dim intFileNumber As Integer
      Dim lngFileSize As Long
      Dim strBuffer As String
      Dim lngCharNumber As Long
      Dim strCharacter As String * 1
      Dim strFileName As String
      
      strFileName = "C:\Windows\System.ini"
      
      'Get the next available File Number
      intFileNumber = FreeFile
      
      DoCmd.Hourglass True
      
      Open strFileName For Binary Access Read Shared As #intFileNumber
      
      lngFileSize = LOF(intFileNumber)    'How large is the File in Bytes?
      strBuffer = Space$(lngFileSize)     'Set Buffer Size to File Length
      
      Get #intFileNumber, , strBuffer     'Grab a Chunk of Data from the File
      Close #intFileNumber
      
      'Display results on a Byte-by-Byte basic
      For lngCharNumber = 1 To lngFileSize
        strCharacter = Mid(strBuffer, lngCharNumber, 1)
        Debug.Print Format(lngCharNumber, "0000000") & " = Decimal(" & Format(Asc(strCharacter), "000") & _
                    "), Hexidecimal(" & Hex$(Asc(strCharacter)) & "), Character[" & strCharacter & "]"
      Next
      
      DoCmd.Hourglass False
      Partial OUTPUT:
      Code:
      0000056 = Decimal(116), Hexidecimal(74), Character[t]
      0000057 = Decimal(105), Hexidecimal(69), Character[i]
      0000058 = Decimal(109), Hexidecimal(6D), Character[m]
      0000059 = Decimal(101), Hexidecimal(65), Character[e]
      0000060 = Decimal(114), Hexidecimal(72), Character[r]
      0000061 = Decimal(061), Hexidecimal(3D), Character[=]
      0000062 = Decimal(116), Hexidecimal(74), Character[t]
      0000063 = Decimal(105), Hexidecimal(69), Character[i]
      0000064 = Decimal(109), Hexidecimal(6D), Character[m]
      0000065 = Decimal(101), Hexidecimal(65), Character[e]
      0000066 = Decimal(114), Hexidecimal(72), Character[r]
      0000067 = Decimal(046), Hexidecimal(2E), Character[.]
      0000068 = Decimal(100), Hexidecimal(64), Character[d]
      0000069 = Decimal(114), Hexidecimal(72), Character[r]
      0000070 = Decimal(118), Hexidecimal(76), Character[v]
      0000071 = Decimal(013), Hexidecimal(D), Character[
      ]
      0000072 = Decimal(010), Hexidecimal(A), Character[
      ]
      0000073 = Decimal(013), Hexidecimal(D), Character[
      ]
      0000074 = Decimal(010), Hexidecimal(A), Character[
      ]
      0000075 = Decimal(091), Hexidecimal(5B), Character[[]
      0000076 = Decimal(109), Hexidecimal(6D), Character[m]
      0000077 = Decimal(099), Hexidecimal(63), Character[c]
      0000078 = Decimal(105), Hexidecimal(69), Character[i]
      0000079 = Decimal(093), Hexidecimal(5D), Character[]]
      0000080 = Decimal(013), Hexidecimal(D), Character[
      ]
      0000081 = Decimal(010), Hexidecimal(A), Character[
      ]
      0000082 = Decimal(091), Hexidecimal(5B), Character[[]
      0000083 = Decimal(100), Hexidecimal(64), Character[d]
      0000084 = Decimal(114), Hexidecimal(72), Character[r]
      0000085 = Decimal(105), Hexidecimal(69), Character[i]
      0000086 = Decimal(118), Hexidecimal(76), Character[v]
      0000087 = Decimal(101), Hexidecimal(65), Character[e]
      0000088 = Decimal(114), Hexidecimal(72), Character[r]
      0000089 = Decimal(051), Hexidecimal(33), Character[3]
      0000090 = Decimal(050), Hexidecimal(32), Character[2]
      0000091 = Decimal(093), Hexidecimal(5D), Character[]]
      0000092 = Decimal(013), Hexidecimal(D), Character[
      ]
      0000093 = Decimal(010), Hexidecimal(A), Character[
      ]
      0000094 = Decimal(091), Hexidecimal(5B), Character[[]
      0000095 = Decimal(051), Hexidecimal(33), Character[3]
      0000096 = Decimal(056), Hexidecimal(38), Character[8]
      0000097 = Decimal(054), Hexidecimal(36), Character[6]
      0000098 = Decimal(101), Hexidecimal(65), Character[e]
      0000099 = Decimal(110), Hexidecimal(6E), Character[n]
      0000100 = Decimal(104), Hexidecimal(68), Character[h]
      0000101 = Decimal(093), Hexidecimal(5D), Character[]]
      0000102 = Decimal(013), Hexidecimal(D), Character[
      ]
      0000103 = Decimal(010), Hexidecimal(A), Character[
      ]
      0000104 = Decimal(119), Hexidecimal(77), Character[w]
      0000105 = Decimal(111), Hexidecimal(6F), Character[o]
      0000106 = Decimal(097), Hexidecimal(61), Character[a]
      0000107 = Decimal(102), Hexidecimal(66), Character[f]
      0000108 = Decimal(111), Hexidecimal(6F), Character[o]
      0000109 = Decimal(110), Hexidecimal(6E), Character[n]
      0000110 = Decimal(116), Hexidecimal(74), Character[t]
      0000111 = Decimal(061), Hexidecimal(3D), Character[=]
      0000112 = Decimal(100), Hexidecimal(64), Character[d]
      0000113 = Decimal(111), Hexidecimal(6F), Character[o]
      0000114 = Decimal(115), Hexidecimal(73), Character[s]
      0000115 = Decimal(097), Hexidecimal(61), Character[a]
      0000116 = Decimal(112), Hexidecimal(70), Character[p]
      0000117 = Decimal(112), Hexidecimal(70), Character[p]
      0000118 = Decimal(046), Hexidecimal(2E), Character[.]
      0000119 = Decimal(070), Hexidecimal(46), Character[F]
      0000120 = Decimal(079), Hexidecimal(4F), Character[O]
      0000121 = Decimal(078), Hexidecimal(4E), Character[N]
      0000122 = Decimal(013), Hexidecimal(D), Character[
      ]
      0000123 = Decimal(010), Hexidecimal(A), Character[
      ]
      0000124 = Decimal(069), Hexidecimal(45), Character[E]
      0000125 = Decimal(071), Hexidecimal(47), Character[G]
      0000126 = Decimal(065), Hexidecimal(41), Character[A]
      0000127 = Decimal(056), Hexidecimal(38), Character[8]
      0000128 = Decimal(048), Hexidecimal(30), Character[0]
      0000129 = Decimal(087), Hexidecimal(57), Character[W]
      0000130 = Decimal(079), Hexidecimal(4F), Character[O]
      0000131 = Decimal(065), Hexidecimal(41), Character[A]
      0000132 = Decimal(046), Hexidecimal(2E), Character[.]
      0000133 = Decimal(070), Hexidecimal(46), Character[F]
      0000134 = Decimal(079), Hexidecimal(4F), Character[O]
      0000135 = Decimal(078), Hexidecimal(4E), Character[N]
      0000136 = Decimal(061), Hexidecimal(3D), Character[=]
      0000137 = Decimal(069), Hexidecimal(45), Character[E]
      0000138 = Decimal(071), Hexidecimal(47), Character[G]
      0000139 = Decimal(065), Hexidecimal(41), Character[A]
      0000140 = Decimal(056), Hexidecimal(38), Character[8]
      0000141 = Decimal(048), Hexidecimal(30), Character[0]
      0000142 = Decimal(087), Hexidecimal(57), Character[W]
      0000143 = Decimal(079), Hexidecimal(4F), Character[O]
      0000144 = Decimal(065), Hexidecimal(41), Character[A]
      0000145 = Decimal(046), Hexidecimal(2E), Character[.]
      0000146 = Decimal(070), Hexidecimal(46), Character[F]
      0000147 = Decimal(079), Hexidecimal(4F), Character[O]
      0000148 = Decimal(078), Hexidecimal(4E), Character[N]
      0000149 = Decimal(013), Hexidecimal(D), Character[
      ]
      0000150 = Decimal(010), Hexidecimal(A), Character[
      ]
      0000151 = Decimal(069), Hexidecimal(45), Character[E]

      Comment

      • wquatan
        New Member
        • Oct 2007
        • 48

        #4
        Hi,

        Thank you for the interesting example !!

        However, there is one problem : I'm unable to read the whole file into one strBuffer. The files I have to threat can be very very large.

        I really need to read byte per byte till the end is reached.

        Thx

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32663

          #5
          You should find all you need in post #2. It's not all done for you, but I don't imagine that should be a problem.

          ADezii's post is an example of what can be done with it. You may find some good example code in there.

          You have all you need though to program it as you require.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32663

            #6
            Originally posted by wquatan
            I really need to read byte per byte till the end is reached.
            What am I talking about?!?

            I just read through ADezii's code and it does exactly that for you :S Furthermore it illustrates clearly what it's doing with the output as displayed.

            I'm confused as to what the problem is.

            PS. I'm no longer confused. I see the limitation. It assumes that the whole file is small enough to fit into a string buffer. It should be fairly straightforward to build from this a version which handles the whole thing if less than a certain size, but breaks it down to multiples of your maximum buffer size if it exceeds that surely.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32663

              #7
              Alternatively, if buffered input is beyond your scope, direct input of each character is still quite plausible. It is all buffered by the operating system anyway. All the building blocks are introduced and there are even examples of how to use them.

              We can probably help in more detail still, but are you sure you want us to?

              Comment

              • wquatan
                New Member
                • Oct 2007
                • 48

                #8
                Originally posted by NeoPa
                It assumes that the whole file is small enough to fit into a string buffer.
                Yep, that's the point.

                And what I don't know (yet) is how to indicate to Get # that only one byte needs to be read, and at the next Get # that the second byte must be read a.s.o. I assume eof() can be used to detect the end.

                Comment

                • wquatan
                  New Member
                  • Oct 2007
                  • 48

                  #9
                  Originally posted by NeoPa
                  We can probably help in more detail still, but are you sure you want us to?
                  What sort of remark is this ?

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32663

                    #10
                    Originally posted by wquatan
                    Yep, that's the point.

                    And what I don't know (yet) is how to indicate to Get # that only one byte needs to be read, and at the next Get # that the second byte must be read a.s.o. I assume eof() can be used to detect the end.
                    The amount of data read in with a Get# statement is equal to the number of characters already in the variable passed. But you would know this already if you'd simply checked out the resources I pointed you to in my earlier post.

                    Determining the size of the file, as already shown in ADezii's code, can be done with the LOF() function.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32663

                      #11
                      Originally posted by wquatan
                      What sort of remark is this ?
                      Since you ask - I thought you might be worried that it would appear you required spoon-feeding.

                      The answers are all there. It only takes a little effort to read through and find what you need.

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32663

                        #12
                        I'm gonna take out that last part.

                        I reread through what you'd posted, and it probably didn't warrant such a response.

                        I'm still not exactly impressed, but what you wrote was not insulting of ADezii (as I'd misread it to be). What is left there now I do hold to be appropriate.

                        Comment

                        • wquatan
                          New Member
                          • Oct 2007
                          • 48

                          #13
                          Originally posted by NeoPa
                          Since you ask - I thought you might be worried that it would appear you required spoon-feeding.

                          The answers are all there. It only takes a little effort to read through and find what you need.
                          Attitude ? Which attitude ? Of the Administrator ?

                          I just wanted to know
                          - a fast example to read byte by byte (in the assumption) there might be different solutions with differences in processing speed or system-impact
                          - I was very pleased with the example given by ADezii but wasn't aware how to indicate the Get # to read one byte

                          Meaning, everything went fine, till a certain Administrator came in

                          EOD

                          Comment

                          • wquatan
                            New Member
                            • Oct 2007
                            • 48

                            #14
                            Originally posted by wquatan
                            Attitude ? Which attitude ? Of the Administrator ?

                            I just wanted to know
                            - a fast example to read byte by byte (in the assumption) there might be different solutions with differences in processing speed or system-impact
                            - I was very pleased with the example given by ADezii but wasn't aware how to indicate the Get # to read one byte

                            Meaning, everything went fine, till a certain Administrator came in

                            EOD
                            The above reply was related to the removed content of the Reply.

                            Be sure, that before posting any question, I try to do my homework by searching around. Google is my friend. But I didn't know the Get# command, probably the reason why I couldn't find anything.

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32663

                              #15
                              Originally posted by wquatan
                              The above reply was related to the removed content of the Reply.
                              I can appreciate that, without an administrator's ability to change your mind retrospectively , and the fact that I did, you deserve to be cut some slack on that. Consider it cut.
                              Originally posted by wquatan
                              Be sure, that before posting any question, I try to do my homework by searching around. Google is my friend. But I didn't know the Get# command, probably the reason why I couldn't find anything.
                              I appreciate that attitude.

                              I'm guessing from this that you never noticed post #2.

                              It was from how you behaved after that, that I got the impression you weren't bothering to check out what was posted. Clearly if you missed it, the rest must be viewed from an entirely different angle.

                              Comment

                              Working...