Solved

Read a file in binary

Posted on 1997-08-05
9
202 Views
Last Modified: 2010-08-05
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!
 
0
Comment
Question by:YamSeng
  • 5
  • 3
9 Comments
 

Expert Comment

by:yan051197
ID: 1252879
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.

Hope this is helpful.

0
 
LVL 1

Author Comment

by:YamSeng
ID: 1252880
can show example to operate on each byte and then retreiving every bit from every single byte?
0
 
LVL 1

Author Comment

by:YamSeng
ID: 1252881
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.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 4

Expert Comment

by:Woodster
ID: 1252882
If you just need to XOR a byte, do this:

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

readValue = ...;
xorValue = ...;

result = readValue ^ xorValue;

and result will hold the desired value.

0
 
LVL 4

Expert Comment

by:Woodster
ID: 1252883
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().
0
 
LVL 4

Accepted Solution

by:
Woodster earned 50 total points
ID: 1252884
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.
0
 
LVL 4

Expert Comment

by:Woodster
ID: 1252885
So, if I want to check the nth value should I use

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

?

0
 
LVL 1

Author Comment

by:YamSeng
ID: 1252886
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.

0
 
LVL 4

Expert Comment

by:Woodster
ID: 1252887
Thanks for being helpful.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Statment to match two tables in c# 6 79
chcp 65001 File encoding 66 247
Passing command line arguments in C 10 93
Need example 5 116
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question