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
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.
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. - 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: - 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]
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
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.
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.
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?
@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.
@NeoPa
What sort of remark is this ?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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.
...
|
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
|
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.
|
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...
| |
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~
|
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.
|
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;
|
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...
|
by: sam |
last post by:
same as subject?
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |