473,667 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read File Byte by Byte

48 New Member
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
Feb 22 '09 #1
18 23465
NeoPa
32,568 Recognized Expert Moderator MVP
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.
Feb 22 '09 #2
ADezii
8,834 Recognized Expert Expert
@wquatan
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.
Expand|Select|Wrap|Line Numbers
  1. Dim intFileNumber As Integer
  2. Dim lngFileSize As Long
  3. Dim strBuffer As String
  4. Dim lngCharNumber As Long
  5. Dim strCharacter As String * 1
  6. Dim strFileName As String
  7.  
  8. strFileName = "C:\Windows\System.ini"
  9.  
  10. 'Get the next available File Number
  11. intFileNumber = FreeFile
  12.  
  13. DoCmd.Hourglass True
  14.  
  15. Open strFileName For Binary Access Read Shared As #intFileNumber
  16.  
  17. lngFileSize = LOF(intFileNumber)    'How large is the File in Bytes?
  18. strBuffer = Space$(lngFileSize)     'Set Buffer Size to File Length
  19.  
  20. Get #intFileNumber, , strBuffer     'Grab a Chunk of Data from the File
  21. Close #intFileNumber
  22.  
  23. 'Display results on a Byte-by-Byte basic
  24. For lngCharNumber = 1 To lngFileSize
  25.   strCharacter = Mid(strBuffer, lngCharNumber, 1)
  26.   Debug.Print Format(lngCharNumber, "0000000") & " = Decimal(" & Format(Asc(strCharacter), "000") & _
  27.               "), Hexidecimal(" & Hex$(Asc(strCharacter)) & "), Character[" & strCharacter & "]"
  28. Next
  29.  
  30. DoCmd.Hourglass False
Partial OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. 0000056 = Decimal(116), Hexidecimal(74), Character[t]
  2. 0000057 = Decimal(105), Hexidecimal(69), Character[i]
  3. 0000058 = Decimal(109), Hexidecimal(6D), Character[m]
  4. 0000059 = Decimal(101), Hexidecimal(65), Character[e]
  5. 0000060 = Decimal(114), Hexidecimal(72), Character[r]
  6. 0000061 = Decimal(061), Hexidecimal(3D), Character[=]
  7. 0000062 = Decimal(116), Hexidecimal(74), Character[t]
  8. 0000063 = Decimal(105), Hexidecimal(69), Character[i]
  9. 0000064 = Decimal(109), Hexidecimal(6D), Character[m]
  10. 0000065 = Decimal(101), Hexidecimal(65), Character[e]
  11. 0000066 = Decimal(114), Hexidecimal(72), Character[r]
  12. 0000067 = Decimal(046), Hexidecimal(2E), Character[.]
  13. 0000068 = Decimal(100), Hexidecimal(64), Character[d]
  14. 0000069 = Decimal(114), Hexidecimal(72), Character[r]
  15. 0000070 = Decimal(118), Hexidecimal(76), Character[v]
  16. 0000071 = Decimal(013), Hexidecimal(D), Character[
  17. ]
  18. 0000072 = Decimal(010), Hexidecimal(A), Character[
  19. ]
  20. 0000073 = Decimal(013), Hexidecimal(D), Character[
  21. ]
  22. 0000074 = Decimal(010), Hexidecimal(A), Character[
  23. ]
  24. 0000075 = Decimal(091), Hexidecimal(5B), Character[[]
  25. 0000076 = Decimal(109), Hexidecimal(6D), Character[m]
  26. 0000077 = Decimal(099), Hexidecimal(63), Character[c]
  27. 0000078 = Decimal(105), Hexidecimal(69), Character[i]
  28. 0000079 = Decimal(093), Hexidecimal(5D), Character[]]
  29. 0000080 = Decimal(013), Hexidecimal(D), Character[
  30. ]
  31. 0000081 = Decimal(010), Hexidecimal(A), Character[
  32. ]
  33. 0000082 = Decimal(091), Hexidecimal(5B), Character[[]
  34. 0000083 = Decimal(100), Hexidecimal(64), Character[d]
  35. 0000084 = Decimal(114), Hexidecimal(72), Character[r]
  36. 0000085 = Decimal(105), Hexidecimal(69), Character[i]
  37. 0000086 = Decimal(118), Hexidecimal(76), Character[v]
  38. 0000087 = Decimal(101), Hexidecimal(65), Character[e]
  39. 0000088 = Decimal(114), Hexidecimal(72), Character[r]
  40. 0000089 = Decimal(051), Hexidecimal(33), Character[3]
  41. 0000090 = Decimal(050), Hexidecimal(32), Character[2]
  42. 0000091 = Decimal(093), Hexidecimal(5D), Character[]]
  43. 0000092 = Decimal(013), Hexidecimal(D), Character[
  44. ]
  45. 0000093 = Decimal(010), Hexidecimal(A), Character[
  46. ]
  47. 0000094 = Decimal(091), Hexidecimal(5B), Character[[]
  48. 0000095 = Decimal(051), Hexidecimal(33), Character[3]
  49. 0000096 = Decimal(056), Hexidecimal(38), Character[8]
  50. 0000097 = Decimal(054), Hexidecimal(36), Character[6]
  51. 0000098 = Decimal(101), Hexidecimal(65), Character[e]
  52. 0000099 = Decimal(110), Hexidecimal(6E), Character[n]
  53. 0000100 = Decimal(104), Hexidecimal(68), Character[h]
  54. 0000101 = Decimal(093), Hexidecimal(5D), Character[]]
  55. 0000102 = Decimal(013), Hexidecimal(D), Character[
  56. ]
  57. 0000103 = Decimal(010), Hexidecimal(A), Character[
  58. ]
  59. 0000104 = Decimal(119), Hexidecimal(77), Character[w]
  60. 0000105 = Decimal(111), Hexidecimal(6F), Character[o]
  61. 0000106 = Decimal(097), Hexidecimal(61), Character[a]
  62. 0000107 = Decimal(102), Hexidecimal(66), Character[f]
  63. 0000108 = Decimal(111), Hexidecimal(6F), Character[o]
  64. 0000109 = Decimal(110), Hexidecimal(6E), Character[n]
  65. 0000110 = Decimal(116), Hexidecimal(74), Character[t]
  66. 0000111 = Decimal(061), Hexidecimal(3D), Character[=]
  67. 0000112 = Decimal(100), Hexidecimal(64), Character[d]
  68. 0000113 = Decimal(111), Hexidecimal(6F), Character[o]
  69. 0000114 = Decimal(115), Hexidecimal(73), Character[s]
  70. 0000115 = Decimal(097), Hexidecimal(61), Character[a]
  71. 0000116 = Decimal(112), Hexidecimal(70), Character[p]
  72. 0000117 = Decimal(112), Hexidecimal(70), Character[p]
  73. 0000118 = Decimal(046), Hexidecimal(2E), Character[.]
  74. 0000119 = Decimal(070), Hexidecimal(46), Character[F]
  75. 0000120 = Decimal(079), Hexidecimal(4F), Character[O]
  76. 0000121 = Decimal(078), Hexidecimal(4E), Character[N]
  77. 0000122 = Decimal(013), Hexidecimal(D), Character[
  78. ]
  79. 0000123 = Decimal(010), Hexidecimal(A), Character[
  80. ]
  81. 0000124 = Decimal(069), Hexidecimal(45), Character[E]
  82. 0000125 = Decimal(071), Hexidecimal(47), Character[G]
  83. 0000126 = Decimal(065), Hexidecimal(41), Character[A]
  84. 0000127 = Decimal(056), Hexidecimal(38), Character[8]
  85. 0000128 = Decimal(048), Hexidecimal(30), Character[0]
  86. 0000129 = Decimal(087), Hexidecimal(57), Character[W]
  87. 0000130 = Decimal(079), Hexidecimal(4F), Character[O]
  88. 0000131 = Decimal(065), Hexidecimal(41), Character[A]
  89. 0000132 = Decimal(046), Hexidecimal(2E), Character[.]
  90. 0000133 = Decimal(070), Hexidecimal(46), Character[F]
  91. 0000134 = Decimal(079), Hexidecimal(4F), Character[O]
  92. 0000135 = Decimal(078), Hexidecimal(4E), Character[N]
  93. 0000136 = Decimal(061), Hexidecimal(3D), Character[=]
  94. 0000137 = Decimal(069), Hexidecimal(45), Character[E]
  95. 0000138 = Decimal(071), Hexidecimal(47), Character[G]
  96. 0000139 = Decimal(065), Hexidecimal(41), Character[A]
  97. 0000140 = Decimal(056), Hexidecimal(38), Character[8]
  98. 0000141 = Decimal(048), Hexidecimal(30), Character[0]
  99. 0000142 = Decimal(087), Hexidecimal(57), Character[W]
  100. 0000143 = Decimal(079), Hexidecimal(4F), Character[O]
  101. 0000144 = Decimal(065), Hexidecimal(41), Character[A]
  102. 0000145 = Decimal(046), Hexidecimal(2E), Character[.]
  103. 0000146 = Decimal(070), Hexidecimal(46), Character[F]
  104. 0000147 = Decimal(079), Hexidecimal(4F), Character[O]
  105. 0000148 = Decimal(078), Hexidecimal(4E), Character[N]
  106. 0000149 = Decimal(013), Hexidecimal(D), Character[
  107. ]
  108. 0000150 = Decimal(010), Hexidecimal(A), Character[
  109. ]
  110. 0000151 = Decimal(069), Hexidecimal(45), Character[E]
Feb 22 '09 #3
wquatan
48 New Member
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
Feb 22 '09 #4
NeoPa
32,568 Recognized Expert Moderator MVP
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.
Feb 22 '09 #5
NeoPa
32,568 Recognized Expert Moderator MVP
@wquatan
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.
Feb 22 '09 #6
NeoPa
32,568 Recognized Expert Moderator MVP
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?
Feb 22 '09 #7
wquatan
48 New Member
@NeoPa
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.
Feb 22 '09 #8
wquatan
48 New Member
@NeoPa
What sort of remark is this ?
Feb 22 '09 #9
NeoPa
32,568 Recognized Expert Moderator MVP
@wquatan
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.
Feb 22 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
8653
by: Lingyun Yang | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Dear all, I have a file it's binary data viewed in UltraEdit is EF BB BF 0D 0A 3C ....... I want to read them into a int or long int array byte for example: byte=0xEFBB byte=0xBF0D
5
2922
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
35
11418
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
8
18647
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in adv.
6
5797
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100 bytes of the file and some of these files are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the largest files and the other files take "forever" to get the last 100 bytes. This is the code I have currently that works with...
0
5778
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read 17640 every times., and i set byte read=new byte, i have the wav format paper, but i don't know how to read the data correctly into the byte array, and used for doing DFT.... here's my code, and i am little hurry...thanks for the helps first~
14
11630
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the correct way to get it into a memory stream. Any help appreciated.
10
2050
by: Arquitecto | last post by:
Hi , I have a question about a file operation i want to do . I have a data file lets say X bytes . I want to read the file and delete a byte every 2nd byte . I am a little comfused ,my approach is like this . main() { char buf; FILE *fp;
5
11254
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
3
8904
by: sam | last post by:
same as subject?
0
8458
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8565
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.