Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read file byte for byte

Status
Not open for further replies.

anders75

Programmer
Nov 13, 2002
8
SE
Hi list,

I would like to read a (text) file bytewise, that is, I don't want to read entire lines or strings. How do I do this?

Thanks,
Anders
 
You could try opening the file in binary mode

OPEN file$ [FOR mode] [ACCESS access] [lock] AS [#]filenumber% [LEN=reclen%]

BINARY specifies a binary-file mode. In binary mode, you can read or write information to any byte position in the file.

You could look at msdn.microsoft.com for further information...
 
Or read the entire fil into a byte array:

--------------------
Dim BArr() as byte, f as byte
f=freefile
Open "MyFile.txt" for Binary access read as #f
redim Barr(lof(f))
get #f,,Barr
close #f
'Barr is now a byte array of the file
--------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I have done this:

Open "A.doc" For Binary Access Read As filenumber

But I would like to open the file inside a loop, to be able to get each byte separately. Something like:

Open file
do{
Read one byte
Some code here...
}While (BytesRead != 0)

Thanks,
Anders
 
I've used this before to copy file byte by byte. Perhaps it'd help you along the way

Open "c:\mijn documenten\mark.xls" For Binary As #1
Open "c:\mijn documenten\test.xls" For Binary As #2

Dim chara As Byte

Do While Not EOF(1)
Get #1, , chara
Put #2, , chara
Loop

Close #2
Close #1
--------------------------------------
It's not the monsters under your bed, it is the men next door.
That make you fear, make you cry. Make you cry for the Child.
All the wars are fought amongst those lonely men. Unharmed, unscarred.
 
anders75
Which language are you programming in (it looks like c or java...)?

Try
--------------------
Dim BArr() as byte, f as byte, i as long
f=freefile
Open "MyFile.txt" for Binary access read as #f
redim Barr(lof(f))
get #f,,Barr
close #f
i=0
do
f=Barr(i)
i=i+1
if i>Ubound(barr) then msgbox "all bytes are zero in file!": exit do
loop until f<>0
--------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Well.. The thing is that I would like to write VB code that works as the following C++ code:

...
...
FILE* f;
int BytesRead, i;
unsigned char Buffer[BufferSize];
...
...
do
{
BytesRead = 0;
BytesRead = fread(Buffer, 1, sizeof(Buffer), f);
if (! SubjectSearchProcessData(Buffer, BytesRead))
{
cout<<&quot;Subject Search is not started&quot;<<NL;
cout<<&quot;Press <Enter> to exit&quot;<<flush; getchar();
exit(0);
}
} while (BytesRead != 0);
...
...

Could anyone help please me out with this?

Thanks,
Anders
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top