473,499 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Nov 20 '05 #1
12 2287
Cor
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
Nov 20 '05 #2
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.

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Op*************@tk2msftngp13.phx.gbl...
"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb
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
As Cor stated, it is not possible to write a single bit. You could use a
Boolean-Array as a buffer containing 8 values. Wrap the buffer in a class,
add a Write method that writes the passed bit (represented by a Boolean)

at the next position in the buffer and increases the buffer pointer. When 8
bits have been written, create a byte from the 8 bits and write the byte to the file.

I don't know if it makes sense. Be careful because 2 bits will be losted
when writing 10 bits. ;-))
--
Armin

Nov 20 '05 #3
Hello,

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #4
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
Hello,

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet

Nov 20 '05 #5
ROFL
Nov 20 '05 #6
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
Nov 20 '05 #7
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!!)

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> wrote in message
news:ui**************@TK2MSFTNGP10.phx.gbl...
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
Hello,

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


Nov 20 '05 #8
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
"Nice Chap" <Ni******@PlasmaDyne.com> wrote in message
news:eV**************@TK2MSFTNGP12.phx.gbl...
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!!)

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> wrote in message
news:ui**************@TK2MSFTNGP10.phx.gbl...
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
Hello,

"Christian Blackburn" <Christian@Damn@Sp**@Hotmail.com> schrieb:
> 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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet



Nov 20 '05 #9
Hi Fergus,
If you didn't know already those blocks are called clusters.
Cheers,
Christian

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:uw****************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #10
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
Nov 20 '05 #11
Cor

And a hard-disk crash is a ClusterBomb and the result is a

ClusterF*ck.

My English is not so good, will that give the nicest result a clusterfind
check?
Nov 20 '05 #12
Hi Cor,

|| will that give the nicest result a clusterfind check?

??

Regards,
Fergus
Nov 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
23879
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
15
6260
by: MCondon77 | last post by:
This is probably a common problem, but as a novice programmer I'm trying to figure out how to write 0x0A to a BIN file without having it automatically write 0x0D. Here's an example of the code:...
68
5151
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
5
2400
by: Dara Durum | last post by:
Hi ! I want to create a Process Pool Object. I can hold started processes, and can communicate with them. I tryed with many ipc methods, but every of them have bug or other problem. Sockets...
0
789
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
14028
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
2
3524
by: Thomi Aurel RUAG A | last post by:
Hy I'm using Python 2.4.2 on an ARM (PXA-270) platform (linux-2.6.17). My Goal is to write a list of bytes down to a file (opened in binary mode) in one cycle. The crux is that a '0x0a' (line...
13
18156
by: zach | last post by:
Can someone help me out, I can't figure out what I'm doing wrong to write to a file in binary mode. What's wrong with my code? <?php $fileName = "something.dat"; $string = "This is a...
1
3915
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
7128
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,...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5467
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,...
1
4917
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...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
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...

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.