I have a string in char a[100]=00110010
which is the ascii code of '2'.

Now i want to write this in a text (or binary) file as binary bits.
So that when i open the file in binary mode and read a character char ch
say via

ifstream fin;
fin>>ch;

and then

cout<<ch;

output should be '2' only.

Another question?
Since 'a' is 101 byte array, would

ofstream fout;
fout<<ch;

this code will write 101 characters in the file or only 8 characters?

I didn't understand clearly what do you mean, but i'll explain what I know:

a[100] is an array containing 100 chars, each char represents a byte, then the array is a 100 Byte array.
to read an write in binary ifstream has function called read, and ofstream has one called write.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.