We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# Read a file in binary

on
Medium Priority
224 Views
I would like to know how to open/read a file in binary.  That means I should have a pointer pointing to the first bit.
And also, can I overwrite the bit while I'm reading it?

I'm programming in Win95 using Microsoft Visual C++ 4.0. I just want to prgram in C.

thanks!

Comment
Watch Question

## View Solution Only

Commented:
Open a file as binary mode and write/read:
FILE *fp = fopen(filename, "r+b");

Operation for each bit:
It seems difficult to operate each bit. As I know, we usually
operate for each byte by stream fp. I think you can define a
union to access each bit, then read/write to the file as one
byte.

Commented:
can show example to operate on each byte and then retreiving every bit from every single byte?

Commented:
Actually, I need to do an XOR operation on the byte.  If I can do XOR operation on a byte (given another byte), then I do not need to use the bit.

Commented:
If you just need to XOR a byte, do this:

unsigned char result;     // The resultant value from the XOR.
unsigned char xorValue;   // The value to XOR readValue with.

xorValue = ...;

and result will hold the desired value.

Commented:
By the way, If you still want to access an individual bit, you can do this:

unsigned char initialValue;   // The byte containing the data.
int bitNumber;                // The bit you want to check.
// This will be 1 - 8 where 1 is
// the rightmost bit.

valueOfBit = initialValue & pow(2, bitNumber-1);

valueOfBit will contain either a 1 or a 0 depending on the value of the byte being checked.  You may also need to include math.h for pow().
Commented:
YamSeng - Try out the above comments I have already added and let me know if that helps.  If not, add another comment to this and I may be able to provide come more info.

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
So, if I want to check the nth value should I use

valueOfBit = initialValue & pow(2, bitNumber-n);

?

Commented:
OK - I've just tried it myself and I was slightly wrong before.  This WILL work:

unsigned char initialValue; // The byte containing the data.
int bitNumber;              // The bit you want to check.
// This will be 1 - 8 where 1 is
// the rightmost bit.

initialValue = ...;
bitNumber = ...;
int valueOfBit = initialValue & (int)pow(2, bitNumber-1);

if valueOfBit is 0 then the bit being checked was 0, if it is non-zero, then the bit was set to 1.

So, back to your comment, if you want to check the nth value, you would set bitNumber to be n.

Eg: If you have a byte as follows:
00001101 (13)

A value of 1, 3 and 4 for bitNumber in the above statement would return a non-zero value (bit is set) and values of 2, 5, 6, 7 and 8 would return 0 (bit is not set.

In terms of the nth value:
valueOfBit = initialValue & (int)pow(2, n-1);

Sorry for any confusion from my previous comment.  Again, if you need more info, just add a comment.

Commented:
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile