Is it possible to write bit by bit while in Binary Mode?

  • Thread starter Thread starter Christian Blackburn
  • Start date Start date
C

Christian Blackburn

Hi Gang,
I would like to know how to write a single bit to a file (if that's
possible). I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.
Thanks in Advance for an example or explanation,
Christian Blackburn
 
Hi Cristan
Yes you can write a little bit to a file, but when you mean 1/8 byte than
it's impossible.
I would like to know how to write a single bit to a file (if that's
possible). I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.
I hope this helps a 1/8 byte.
Cor
 
Just to add to Armin's suggestion, you need to also write the length of data
to the file somewhere(first byte??), read this and then read the relevent
bits of data from the remainder of the file.
 
Hello,

Christian Blackburn said:
I would like to know how to write a single bit to a file (if that's
possible).

You can only write bytes.
I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.

You may want to have a look at the 'BitArray' class.
 
Hi Guys,
Thanks for all the help. The way I have it right now I have a procedure
that converts a string "01001000" to a single byte and then I write that to
a file. I guess it makes sense that you can't write bit by bit, because at
some point you'd have to make sure the length was divisible by 8. The
problem being a file is always composed of the same pieces and those pieces
being some quantity of whole bytes.

Thanks for the suggestions it would definitely be faster and more memory
efficient to use a BitArray or Boolean Array. So I will try the BitArray
and if I don't like working with it then I'll just revert back to good old
Boolean :).

Also I think NiceChaps is incorrect you don't have to specify the file size
anywhere. intFile_Size = FileSystem.FileLen("C:\File.txt") can do that, it
would just be wasted space.

Thank you all very much for the help,
Christian Blackburn
 
Hi Christian,

Strictly speaking you can't even write a single byte! because the OS
writes in blocks. But it's not something we generally have to care about. :-)

Regards,
Fergus
 
Hi Christian,

Read my post carefully, I said you need to write the 'Length of Data'
NOT File Size.
Consider this situation :

Your Data is 0101010101

Your Data Length would be 10bits where as file size in Bytes would be 2
( or 16 bits!!)
 
Hi Nice Chap,
Okay I'm sorry I didn't understand what you had said. Yes you're absolutely
correct. My file uses
WxHB
Where W is the Width of Squares on my table
H is the height of squares

and B is in Bits the value in each square: 0101010101010101010

So yes W * H is my data length :).
Cheers,
Christian
 
Hi Fergus,
If you didn't know already those blocks are called clusters.
Cheers,
Christian
 
Hi Christian,

Sectors, clusters, tracks, cylinders, surfaces, platters, drives. Yes, I
did, thanks. ;-)

And a hard-disk crash is a ClusterBomb and the result is a ClusterF*ck.

Regards,
Fergus
 
Back
Top