Home > Archive > Visual Basic > December 2005 > Reading Files Byte-For-Byte
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Reading Files Byte-For-Byte
|
|
| PulsarSL@gmail.com 2005-12-18, 7:55 am |
| Hey
I want to write a program that opens a file (not necessarily text -
maybe jpeg, maybe exe, etc), that reads in the data byte for byte and
writes each byte to a regular text file.
How can a read a file byte for byte? This is the code I have now:
Open ifilename For Binary As #fnum
ReDim piece(1 To filelength)
Get #fnum, 1, piece
Close #fnum
Is this how I should be doing it?
Thanks
PulsarSL
| |
| Norm Cook 2005-12-18, 7:55 am |
| A bit confusing here. You want to read the file byte by byte but you are
attempting to read the file into an array.
'byte by byte (air code)
Dim fnum as long
dim b as byte
fnum = freefile
Open ifilename For Binary As #fnum
do while not eof(fnum)
get#fnum, , b 'since b is defined as byte, it reads one byte
'do whatever
loop
close fnum
'array
Dim fnum as long
dim b() as byte
fnum = freefile
Open ifilename For Binary As #fnum
redim b(lof(fnum)-1)
get#fnum, , b 'reads the entire file into the array
close fnum
Also a bit about reading non-text data into a text file?
Sounds like it would be the same as renaming the file to .txt.
Perhaps you mean sending the data as hex characters?
<PulsarSL@gmail.com> wrote in message
news:1134904222.929027.266920@f14g2000cwb.googlegroups.com...
> Hey
>
> I want to write a program that opens a file (not necessarily text -
> maybe jpeg, maybe exe, etc), that reads in the data byte for byte and
> writes each byte to a regular text file.
>
> How can a read a file byte for byte? This is the code I have now:
>
> Open ifilename For Binary As #fnum
> ReDim piece(1 To filelength)
> Get #fnum, 1, piece
> Close #fnum
>
> Is this how I should be doing it?
>
> Thanks
>
> PulsarSL
>
| |
| Larry Serflaten 2005-12-18, 6:55 pm |
|
<PulsarSL@gmail.com> wrote
> I want to write a program that opens a file (not necessarily text -
> maybe jpeg, maybe exe, etc), that reads in the data byte for byte and
> writes each byte to a regular text file.
>
> How can a read a file byte for byte? This is the code I have now:
>
> Open ifilename For Binary As #fnum
> ReDim piece(1 To filelength)
> Get #fnum, 1, piece
> Close #fnum
>
> Is this how I should be doing it?
Yes, you don't really want to read the file byte by byte due to low
performance. Read it in as you have, and then while it is in memory,
operate on each byte as you wanted. The array has a one for one,
byte for byte, image of the file and will be easier to access in that
manner.
For a more complete example:
Dim piece() As Byte
Dim fnum As Integer
Dim filename As String
filename = "D:\temp\sample.bmp"
fnum = FreeFile
Open filename For Binary As #fnum
ReDim piece(1 To LOF(fnum)) As Byte
Get #fnum, 1, piece
Close #fnum
' Show bmp header bytes ("BM")
Debug.Print StrConv(LeftB(piece, 2), vbUnicode)
LFS
| |
| PulsarSL@gmail.com 2005-12-18, 6:55 pm |
|
Norm Cook wrote:
> A bit confusing here. You want to read the file byte by byte but you are
> attempting to read the file into an array.
Yea, some code I found did it this way, but since Larry said it's slow,
I guess I'll abandon the array -- it doesn't matter to me in which form
I get the data in... I can write the rest of the program around that.
> 'byte by byte (air code)
> Dim fnum as long
> dim b as byte
> fnum = freefile
> Open ifilename For Binary As #fnum
> do while not eof(fnum)
> get#fnum, , b 'since b is defined as byte, it reads one byte
> 'do whatever
> loop
> close fnum
What is the second argument in the get command? I can't seem to find
documentation on this anywhere.
> Also a bit about reading non-text data into a text file?
> Sounds like it would be the same as renaming the file to .txt.
> Perhaps you mean sending the data as hex characters?
I was trying to make a textual representation of the data for testing
by writing each byte to a comma seperated text file -- Would this work?
Forgive me... this is the first time I've ever used binary
reading/writing.
Thank you,
PulsarSL
| |
|
|
|
|
| Larry Serflaten 2005-12-18, 6:55 pm |
|
<PulsarSL@gmail.com> wrote in message
> Norm Cook wrote:
>
> Yea, some code I found did it this way, but since Larry said it's slow,
> I guess I'll abandon the array --
You have misunderstood my message:
[color=darkred]
> Yes, you don't really want to read the file byte by byte due to low performance.
But you did not post the code for a byte by byte method, you posted code
for a single read operation.
The method you showed was the more performant method, so that was
how you should proceed. Reading byte for byte (on larger files) is going
to be slower due simply to the additional commands required to accomplish
the task. You much rather want to read the whole file in one go, storing it
in an array for easy access. Please read my message again and see if it now
better answers your question. I provided the code example for completeness,
to show how it would look with declared variables and the use of FreeFile.
However, when working with files, error handling is also advised but was not
shown.
I think the misunderstanding came about due to your mentioning reading the
file byte by byte, but showing code that reads the entire file in one shot. Reading
byte for byte requires some looping mechanism, which was also posted, but
is going to be slower, again due to the sheer number of commands needed
to accomplish the task. The difference is not going to be very noticable on
smaller files, but reading the entire file into a single array will make for easier
processing later on, when you need to write out the data. Also note how
my example code converted the first to bytes into printable characters for
the Debug command. While that will work for bytes that fall within the range
of printable characters, you'll need to do something else if your file data falls
outside that range (which image data is certain to do).
LFS
| |
| PulsarSL@gmail.com 2005-12-18, 6:55 pm |
|
Larry Serflaten wrote:
> <PulsarSL@gmail.com> wrote in message
>
>
> You have misunderstood my message:
>
Yeah, I just reread it and I can't figure out what I was thinking, but
it was late :P
Thanks
PulsarSL
| |
| PulsarSL@gmail.com 2005-12-18, 6:55 pm |
|
PulsarSL@gmail.com wrote:
>
> Yeah, I just reread it and I can't figure out what I was thinking, but
> it was late :P
>
For clarification, I'm a night owl.
Anyway, why isn't this working?
-----
Private Sub Command1_Click()
Dim mybyte As Byte
Open "c:\test.jpg" For Binary As #1
Open "c:\test2.jpg" For Binary As #2
Do While Not EOF(1)
Get #1, , mybyte
Put #2, , mybyte
Loop
Close #1
Close #1
End Sub
----
The starting file is 15,457 bytes and the end file is 15,458 bytes.
Where's the fencepost error?
PulsarSL
| |
| PulsarSL@gmail.com 2005-12-18, 6:55 pm |
|
Pulsa...@gmail.com wrote:
> Anyway, why isn't this working?
>
>
> The starting file is 15,457 bytes and the end file is 15,458 bytes.
> Where's the fencepost error?
>
Accidentally close #1 twice, leaving #2 open. As soon as I fixed it,
it started working... Curious. Why would an open file be 1 byte
larger?
PulsarSL
|
|
|
|
|