473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I get bitwise access to a file?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Hello guys,

I am currently runnging Kubuntu 5.10 on an AMD Athlon XP +1600 machine. I am
trying to implement a file class that allows bitwise access to a file.

I have created a byte class which looks like this:

class byte
{

public:
byte();
byte(const byte& rhs);
virtual ~byte();
void set_bit(const unsigned int p_index, const bool p_value);
bool get_bit(const unsigned int p_index) const;
bool operator==(const byte& rhs) const;
byte& operator=(const byte& rhs);

protected:

bool* m_data;

};

I want to do the following thing:
I want to write a function read() that reads a byte from and write(const
byte&) that writes a byte to an already opened file so that I can access
each bit of it. My problem is that I have absolutely no idea how to do this
as I only know about the std::fread() and std::fwrite() functions that only
allow bytewise access to files. Is there anyone who can help me with that?

Thank you very much.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iQIVAwUBQ5MCn9qtd8S+cgRiAQO+ig/+J8BBQRr9x6Wt3ukJLFMsXraunVjzyXu8
XOLKsRhrHTeYhDVTtcINpDwgl7ilzX7+LqbQjrQPuLVGJEtBJm RLayiWiNznN10y
V5Ulgc9WQqU0JLkPGzyh01wWssLDYCbvzyj5AmW5TlPfI2DHK5 bszd5QNgheGxGP
sWDnbVRuuTnJcizG/mUsDgTBtb6NgWitwiWc0NXtR+z9wNkohE3S151w0EaJg/K0
LDQvKvPxGX1Jk0mpqME345KvM6mB1qYyrLLVQaUD96TXNrUfje pmL8hNSpTHau72
WzbSSasgEDEuTKof5cFIHyPrMQrW5GEPyoHTHGlx34F9znL/02ZClOFiQvi18ccJ
6MjQuO6Co2/e4WPMhwmlE142rz+Fz60lpLzfKUanuvyQOthsRqTkqH9YBgtPS 23L
E37jBH571GUvu2gxXNygj4l3/js2SINCu8a3LtMboU2ecz+zV0Pkum0J4XiY5tfi
A3rPCwAZiwku33xVnRIJBvQLexiDOQg2MAcHOHbkbo+QlvvwZM 6LcuWDdGo1chwS
WgDar36xlyUPiXn/wRJBQsw0IAMabbdy1bfQPw2MvHJanzy7Vt7G4Rf19dbXtOL6
6uUiqUQhrd6a2EETGSKyjYWFirNiSMjQXnNzmG4nZlmDvgCY93 yVIdXvNq5ZC/6h
ljusvZCuZgM=
=Fum7
-----END PGP SIGNATURE-----
Dec 4 '05 #1
12 2275

Matthias Kirchhart wrote:
I want to do the following thing:
I want to write a function read() that reads a byte from and write(const
byte&) that writes a byte to an already opened file so that I can access
each bit of it. My problem is that I have absolutely no idea how to do this
as I only know about the std::fread() and std::fwrite() functions that only
allow bytewise access to files. Is there anyone who can help me with that?


You can use file streams
http://www.parashift.com/c++-faq-lit...html#faq-15.12
http://www.cppreference.com/cppio/open.html

Dec 4 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Neelesh Bodas wrote:

Matthias Kirchhart wrote:
I want to do the following thing:
I want to write a function read() that reads a byte from and write(const
byte&) that writes a byte to an already opened file so that I can access
each bit of it. My problem is that I have absolutely no idea how to do
this as I only know about the std::fread() and std::fwrite() functions
that only allow bytewise access to files. Is there anyone who can help me
with that?


You can use file streams
http://www.parashift.com/c++-faq-lit...html#faq-15.12
http://www.cppreference.com/cppio/open.html


Well, as far as I understand this, the ifstream.get() function also only
supports bytewise reading, doesn't it?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iQIVAwUBQ5MPNtqtd8S+cgRiAQOcLQ//avjywf7fdzgRuTZ3nVy9+Mx1HoAPCReQ
D0UTLLulu1r3Vh73FjAFSyqE54HYXUQaurhSoO7wowh6g5OzBL zUmp+OmtIgZNNO
xsgQlqgNa+yfM4OOgX14zQtJsx4LFRasCW/ht6bnNVMBrKvPwcv1siASGUNyyTeb
TJVWNmA186eDskc/rJFQvuE2joD/qz24gCg9Drhf8wuC2gTOwYT+b61gHdKb5P3p
uN93kiI3lOmlK88t4KzVydOqdUiwUWLyyTQ7TBF6NnSq1Y8Abf yLBMlJw4pEWARi
15m6kRdXyi13mAuy59Q5kpw9v9Ss4WS1N5BNJkjNcdKwZ//LLnTJrFD2XdDGPFK4
bTK3RHoOPpTg+ErV5Z6CrvA+1ClZohBcsgmfQh1oXVpOIDRuma aPWkfXyNOOXGLO
xbXGvd2GfBmjT+dFU3pF9Thuuw8IDeo9VzIOes3rOMxVWsdYBJ HccMXwW19vhQTa
YxH2EgOiXYCvId8Al0WkGCA37lymc5vRR5F6Jvvl2AQ5YsQ+XP ZjPAnqHrxtp+gm
27LwnJgcY40b3/3GRPB5L9rVuiDYrbSlmrx4r4Dapciu5ut/dShrCcmQxdVbdeBr
GC5OeevBCpBE9PT7FPNUL82x1WpdNHE9lbXRvk4C76a8Xw4UXt g2VSuMzwjvGvMx
DMnzCayU2GE=
=GIK0
-----END PGP SIGNATURE-----
Dec 4 '05 #3
Matthias Kirchhart wrote:
Neelesh Bodas wrote:
You can use file streams
http://www.parashift.com/c++-faq-lit...html#faq-15.12
http://www.cppreference.com/cppio/open.html
Well, as far as I understand this, the ifstream.get() function also only
supports bytewise reading, doesn't it?


probably I misinterpreted your question - I belive you said that
I want to write a function read() that reads a byte from and write(const
byte&) that writes a byte to an already opened file so that I can access
each bit of it.


Which means that you want to read and write a byte, and you will access
bits from the byte seperate from the IO mechanism

Do you want to say that you want to read individual bits from a byte? I
don't know whether that is possible or not, but most likely it is not.

Sorry for the confusion.

Dec 4 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Neelesh Bodas wrote:
Matthias Kirchhart wrote:
Neelesh Bodas wrote:
> You can use file streams
> http://www.parashift.com/c++-faq-lit...html#faq-15.12
> http://www.cppreference.com/cppio/open.html


Well, as far as I understand this, the ifstream.get() function also only
supports bytewise reading, doesn't it?


probably I misinterpreted your question - I belive you said that
I want to write a function read() that reads a byte from and write(const
byte&) that writes a byte to an already opened file so that I can access
each bit of it.


Which means that you want to read and write a byte, and you will access
bits from the byte seperate from the IO mechanism

Do you want to say that you want to read individual bits from a byte? I
don't know whether that is possible or not, but most likely it is not.

Sorry for the confusion.


Well I guess the confusion is my fault. From now on I will say "cByte" if I
mean my own byte class and "byte" if I mean a byte ;)

So what I want my read function to do is the following:

Read a byte from the file. Then create a cByte object at set the bits in the
boolean array of the cByte object appropriately. Then return it.

The write function gets a cByte and writes it as an byte to the file.

In the end I have a cByte and can access each bit of it. I hope that there
is no more confusion left ;)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iQIVAwUBQ5MbDNqtd8S+cgRiAQMoTg/+M28YDyZbpjvxlwXUEZvgr+wCXyaykXsV
tJAI11+k6pyo5lF0yVytolA7xJEXVLGfo8VwzribCBgXbm866J TiFtyxpwqXavoP
JBFKhtUqVKStHfLkVonltPKRyZ3zYcO4l5KMdiAa6c+vlIdlLu Oi6YUx0Qgc8q9J
XQpXM9vLHK1iabSl3wxOJqjoy+4SXPJ0d6911hpJOh10XGfcav QxBpsz9tuoWovm
tadW0VYTI+h9r8PJy50rA2jRNjHLoLOxLbfOt2KodTQ9mYEeiJ +C2XaZDuoTMfCk
nInINJIuEaNT7x3k7O7U+mF+4a1jrgYYtHVE0/ztFJsSm9I2o68Y7Cgdrn/01Hmq
EWT3I94x2sEnpmL4rx9L40xhmY2esnwriBKQ1SU8V8W0/tGQz7x6FvOjDM8xm+h5
P3E7lpt1FaQW3wPFjLbSNF6Z5isS2TDKzrTqHmu0TDtnQH6J4X YkGNkVWPZOK3bg
uOZ9eae4qPJ+5+ed+Es6NgOZcMwfDn5drF1ZTs0a8opvwwmoJn qJa0DdN1KVvsUO
QuAIqMLe6i+fYSE5E8t9WK7L2jHI2LDPq73g6PBAgx6IbYoD4s QP2xindD7qC5xG
m3GprZGrqVDw3uxtdeuWCu7f55S+K0o4oMSDAmRa0aKD1uyVja Fha6PWuii1n23G
GDz5c1pkzE0=
=7fjC
-----END PGP SIGNATURE-----
Dec 4 '05 #5
Matthias Kirchhart wrote:


Read a byte from the file.
This can be done by using the usual file IO. You will be most likely
reading the byte in, say a char variable. (Though note that C++ std
doesnot guarantee anything about how many bytes should a char occupy.)
Then create a cByte object at set the bits in the boolean array of the cByte object appropriately.
This can be done by a simple for loop.

cByte b;
for(int i=0; i < CHAR_BIT; i++)
b.set_bit(index, ch & ( 1<< (CHAR_BIT-1-i) ) );
The write function gets a cByte and writes it as an byte to the file.
This can be done by a for loop again
char ch;
for(int i=0; i < CHAR_BIT; i++)
ch = ch | 1<< (CHAR_BIT-1-b.get_bit(i) )

[Warning : untested code]

then write ch to file by usual FILE IO.

In the end I have a cByte and can access each bit of it.

As an aside, prefer vector over arrays.

If the question is about any library method to do all this, I have no
idea about that.

Dec 4 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Neelesh Bodas wrote:
Matthias Kirchhart wrote:


Read a byte from the file.


This can be done by using the usual file IO. You will be most likely
reading the byte in, say a char variable. (Though note that C++ std
doesnot guarantee anything about how many bytes should a char occupy.)
Then create a cByte object at set the bits in the boolean array of the
cByte object appropriately.


This can be done by a simple for loop.

cByte b;
for(int i=0; i < CHAR_BIT; i++)
b.set_bit(index, ch & ( 1<< (CHAR_BIT-1-i) ) );
The write function gets a cByte and writes it as an byte to the file.


This can be done by a for loop again
char ch;
for(int i=0; i < CHAR_BIT; i++)
ch = ch | 1<< (CHAR_BIT-1-b.get_bit(i) )

[Warning : untested code]

then write ch to file by usual FILE IO.

In the end I have a cByte and can access each bit of it.

As an aside, prefer vector over arrays.

If the question is about any library method to do all this, I have no
idea about that.


Thank you, that was exactly what I was looking for.

The reading method works, the writing doesn't. After some thinking I found
out that the following works:

char char_data = 0;
for(int i=0; i < 8; i++)
{
if ( p_data.get_bit( i ) )
char_data = char_data | (1 << ( 7 - i) );
}
fwrite(&char_data,1,1,m_file);

(The program is only for me and my system, and there I know that a char is a
byte big)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iQIVAwUBQ5Mxfdqtd8S+cgRiAQOhqxAApnkAAnA/cy6t8df6Ex9NmaCdfijYMSpx
cHYI2MBYro0XUd5AFZM+JrKoWT3bXtgluBxZE+zRAnhfmXtSI0 VHXyk1xZtWIo91
wMkLBphJcf9riSSuU+F9Y6M+FoHSjjv8GGHlPO27qxRWPqrlm1 nszzgeha81AM48
lt5APLY4EqXOjIO67RrMuVEIcf9ncT74IvCSVj9xP0KwbRncz8 D/rbGT4QvszABs
Cx+t3TAUkzw5C/WpspKPk8IGQT6qywvkU8YH1DXu5Zf70VJ+f722fODO8R3fqd6c
Xg2XOIp0GoNuSc+saTlNbSrFpst08fDmAk6O/IbyN2TuT+oUY71xH9G7JNL1YIsR
4A8u5Zg9T3SbgOel3JKXhby7bXO15leViCj4wVHv8a6NIquetP wNxTXEs9ZGm5HM
CmwRyqfps7mCTLrxf/+cYeMDh0N0k5l1OtsxZNe0fRt6JpgRsVT3l02n+7844mpU
QWAB33pU86Wqew8usTHwm/5C6ez2tUW055Y6KlMV+rqVo942WiFXg3kYbmHiNNAR
tOXvfdvenUCXZaNLs3OOcEGsx42tncYWizj6UAOJXLAWA45+EZ TjhMiGd4gsqYca
OGzw/ND18KQR214g3dKKG9dYfrXdtPk9L60fnpW9MuWFnCgSIUNLi9s bNQ3w5QeL
808uD7YOJTo=
=BM2y
-----END PGP SIGNATURE-----
Dec 4 '05 #7
Matthias Kirchhart wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Hello guys,

I am currently runnging Kubuntu 5.10 on an AMD Athlon XP +1600 machine. I am
trying to implement a file class that allows bitwise access to a file.

I have created a byte class which looks like this:

class byte
{

public:
byte();
byte(const byte& rhs);
virtual ~byte();
void set_bit(const unsigned int p_index, const bool p_value);
bool get_bit(const unsigned int p_index) const;
bool operator==(const byte& rhs) const;
byte& operator=(const byte& rhs);

protected:

bool* m_data;

};


Whats wrong with std::bitset<8>?

It'll provide iterator and indexed access to the bits.

Ben Pope
Dec 5 '05 #8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Ben Pope wrote:

Whats wrong with std::bitset<8>?

It'll provide iterator and indexed access to the bits.

Ben Pope

Well there is nothing wrong with it, I just didn't know about it...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iQIVAwUBQ5PXndqtd8S+cgRiAQPg9Q//Q/TQUnxt1ezVkl9yTXEv4YpoU5n9Xmce
gTokqruV09ifG1qIQveyzfR26fXSYS45YaK+wpAzM+LHtwl1tE g6DV5P12MNBgUf
5FElkd2quTIDdcHDt7Rz5X/H7DaFYrocp4XSVjtgs1JVPA7s8cK/3N+PiFEwSrrU
97lb31A/0ZlebgWlZ5N0BlB8Z9RrLm21AqugANTVnbSSmcL9DKA9E5NWSh 5Lzk7W
qgCipji3y23jQX++sF1Yl496vr6ShTtnkx/dLI5wtIev97tmJIB2oXrikC28vjyn
i3fDZ2o6cK803iP7tlzbrxcKxMt8crtI2BS0nQxe1M/Op5p1l9vvaSbnKq8TFkJt
4ZemFvw64bHB6wGCHuyPoiG8itPVpHGH1IgL7bPWWpjT1R0Yrn 9ACQgmKcAErzcU
xOhCowEqYNBu8RE05j6NK+cKcQ1/BAhZgNVi174WfQ2r0fPm0wOzSrBWLyHGDBvv
5awsrhKhrpm4FjiZZPmNyUQWpX3a9UoauU4Br3HOfTCWfmN+Jw sO8mlI6DNz5qcg
AS+0YGCHahUYw5hyw7CpM3J0r7pD8CZP7v/uiH+KU1V74iAvxfjGe2qT/AQIy/df
hr61ZhxiivkOkWVF2TXULk7N0gWWfzROOZWiw+2D8yHqmCBR8a/Ou7bsCr7yiyUx
Npxg7cmP3Cc=
=4FRV
-----END PGP SIGNATURE-----
Dec 5 '05 #9

Neelesh Bodas wrote:
Matthias Kirchhart wrote:


Read a byte from the file.


This can be done by using the usual file IO. You will be most likely
reading the byte in, say a char variable. (Though note that C++ std
doesnot guarantee anything about how many bytes should a char occupy.)


Yes it does. A char always occupies exactly one byte. What is not
defined by the standard is how many bits there are in one byte.

Gavin Deane

Dec 5 '05 #10

de*********@hotmail.com wrote:
Yes it does. A char always occupies exactly one byte. What is not
defined by the standard is how many bits there are in one byte.
Agreed.

Gavin Deane


Dec 5 '05 #11
Matthias Kirchhart wrote:

Ben Pope wrote:
Whats wrong with std::bitset<8>?

It'll provide iterator and indexed access to the bits.

Well there is nothing wrong with it, I just didn't know about it...


;-)

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Dec 5 '05 #12
"Neelesh Bodas" <ne***********@gmail.com> writes:
Matthias Kirchhart wrote:


Read a byte from the file.


This can be done by using the usual file IO. You will be most likely
reading the byte in, say a char variable. (Though note that C++ std
doesnot guarantee anything about how many bytes should a char occupy.)


Yes it does. A char is exactly one byte, and therefor sizeof (char) is 1.
What the standard doesn't guarantee is how many bits a byte contain.

The macro in <climits> named CHAR_BIT, and the the constant
numeric_limits<unsigned char>::digits reflects the number of bits per byte.

/Niklas Norrthon
Dec 5 '05 #13

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

Similar topics

4
by: Mike Hodkin | last post by:
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
5
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
2
by: in_tyrannos | last post by:
First, the file is read to the buffer. Now, I want to access this buffer in the following way. Start index is defined as i = j*size(byte) +k*size(bit) = j*8+k*1. The length of the sequence to be...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.