473,803 Members | 3,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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==(cons t 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)

iQIVAwUBQ5MCn9q td8S+cgRiAQO+ig/+J8BBQRr9x6Wt3u kJLFMsXraunVjzy Xu8
XOLKsRhrHTeYhDV TtcINpDwgl7ilzX 7+LqbQjrQPuLVGJ EtBJmRLayiWiNzn N10y
V5Ulgc9WQqU0JLk PGzyh01wWssLDYC bvzyj5AmW5TlPfI 2DHK5bszd5QNghe GxGP
sWDnbVRuuTnJciz G/mUsDgTBtb6NgWit wiWc0NXtR+z9wNk ohE3S151w0EaJg/K0
LDQvKvPxGX1Jk0m pqME345KvM6mB1q YyrLLVQaUD96TXN rUfjepmL8hNSpTH au72
WzbSSasgEDEuTKo f5cFIHyPrMQrW5G EPyoHTHGlx34F9z nL/02ZClOFiQvi18cc J
6MjQuO6Co2/e4WPMhwmlE142rz +Fz60lpLzfKUanu vyQOthsRqTkqH9Y BgtPS23L
E37jBH571GUvu2g xXNygj4l3/js2SINCu8a3LtMb oU2ecz+zV0Pkum0 J4XiY5tfi
A3rPCwAZiwku33x VnRIJBvQLexiDOQ g2MAcHOHbkbo+Ql vvwZM6LcuWDdGo1 chwS
WgDar36xlyUPiXn/wRJBQsw0IAMabbd y1bfQPw2MvHJanz y7Vt7G4Rf19dbXt OL6
6uUiqUQhrd6a2EE TGSKyjYWFirNiSM jQXnNzmG4nZlmDv gCY93yVIdXvNq5Z C/6h
ljusvZCuZgM=
=Fum7
-----END PGP SIGNATURE-----
Dec 4 '05 #1
12 2304

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)

iQIVAwUBQ5MPNtq td8S+cgRiAQOcLQ//avjywf7fdzgRuTZ 3nVy9+Mx1HoAPCR eQ
D0UTLLulu1r3Vh7 3FjAFSyqE54HYXU QaurhSoO7wowh6g 5OzBLzUmp+OmtIg ZNNO
xsgQlqgNa+yfM4O OgX14zQtJsx4LFR asCW/ht6bnNVMBrKvPwc v1siASGUNyyTeb
TJVWNmA186eDskc/rJFQvuE2joD/qz24gCg9Drhf8wu C2gTOwYT+b61gHd Kb5P3p
uN93kiI3lOmlK88 t4KzVydOqdUiwUW LyyTQ7TBF6NnSq1 Y8AbfyLBMlJw4pE WARi
15m6kRdXyi13mAu y59Q5kpw9v9Ss4W S1N5BNJkjNcdKwZ//LLnTJrFD2XdDGPF K4
bTK3RHoOPpTg+Er V5Z6CrvA+1ClZoh BcsgmfQh1oXVpOI DRumaaPWkfXyNOO XGLO
xbXGvd2GfBmjT+d FU3pF9Thuuw8IDe o9VzIOes3rOMxVW sdYBJHccMXwW19v hQTa
YxH2EgOiXYCvId8 Al0WkGCA37lymc5 vRR5F6Jvvl2AQ5Y sQ+XPZjPAnqHrxt p+gm
27LwnJgcY40b3/3GRPB5L9rVuiDYr bSlmrx4r4Dapciu 5ut/dShrCcmQxdVbdeB r
GC5OeevBCpBE9PT 7FPNUL82x1WpdNH E9lbXRvk4C76a8X w4UXtg2VSuMzwjv GvMx
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)

iQIVAwUBQ5MbDNq td8S+cgRiAQMoTg/+M28YDyZbpjvxlw XUEZvgr+wCXyayk XsV
tJAI11+k6pyo5lF 0yVytolA7xJEXVL Gfo8VwzribCBgXb m866JTiFtyxpwqX avoP
JBFKhtUqVKStHfL kVonltPKRyZ3zYc O4l5KMdiAa6c+vl IdlLuOi6YUx0Qgc 8q9J
XQpXM9vLHK1iabS l3wxOJqjoy+4SXP J0d6911hpJOh10X GfcavQxBpsz9tuo Wovm
tadW0VYTI+h9r8P Jy50rA2jRNjHLoL OxLbfOt2KodTQ9m YEeiJ+C2XaZDuoT MfCk
nInINJIuEaNT7x3 k7O7U+mF+4a1jrg YYtHVE0/ztFJsSm9I2o68Y7 Cgdrn/01Hmq
EWT3I94x2sEnpmL 4rx9L40xhmY2esn wriBKQ1SU8V8W0/tGQz7x6FvOjDM8x m+h5
P3E7lpt1FaQW3wP FjLbSNF6Z5isS2T DKzrTqHmu0TDtnQ H6J4XYkGNkVWPZO K3bg
uOZ9eae4qPJ+5+e d+Es6NgOZcMwfDn 5drF1ZTs0a8opvw wmoJnqJa0DdN1KV vsUO
QuAIqMLe6i+fYSE 5E8t9WK7L2jHI2L DPq73g6PBAgx6Ib YoD4sQP2xindD7q C5xG
m3GprZGrqVDw3ux tdeuWCu7f55S+K0 o4oMSDAmRa0aKD1 uyVjaFha6PWuii1 n23G
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_da ta,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)

iQIVAwUBQ5Mxfdq td8S+cgRiAQOhqx AApnkAAnA/cy6t8df6Ex9NmaC dfijYMSpx
cHYI2MBYro0XUd5 AFZM+JrKoWT3bXt gluBxZE+zRAnhfm XtSI0VHXyk1xZtW Io91
wMkLBphJcf9riSS uU+F9Y6M+FoHSjj v8GGHlPO27qxRWP qrlm1nszzgeha81 AM48
lt5APLY4EqXOjIO 67RrMuVEIcf9ncT 74IvCSVj9xP0Kwb Rncz8D/rbGT4QvszABs
Cx+t3TAUkzw5C/WpspKPk8IGQT6qy wvkU8YH1DXu5Zf7 0VJ+f722fODO8R3 fqd6c
Xg2XOIp0GoNuSc+ saTlNbSrFpst08f DmAk6O/IbyN2TuT+oUY71x H9G7JNL1YIsR
4A8u5Zg9T3SbgOe l3JKXhby7bXO15l eViCj4wVHv8a6NI quetPwNxTXEs9ZG m5HM
CmwRyqfps7mCTLr xf/+cYeMDh0N0k5l1O tsxZNe0fRt6JpgR sVT3l02n+7844mp U
QWAB33pU86Wqew8 usTHwm/5C6ez2tUW055Y6K lMV+rqVo942WiFX g3kYbmHiNNAR
tOXvfdvenUCXZaN Ls3OOcEGsx42tnc YWizj6UAOJXLAWA 45+EZTjhMiGd4gs qYca
OGzw/ND18KQR214g3dKK G9dYfrXdtPk9L60 fnpW9MuWFnCgSIU NLi9sbNQ3w5QeL
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==(cons t 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)

iQIVAwUBQ5PXndq td8S+cgRiAQPg9Q//Q/TQUnxt1ezVkl9yT XEv4YpoU5n9Xmce
gTokqruV09ifG1q IQveyzfR26fXSYS 45YaK+wpAzM+LHt wl1tEg6DV5P12MN BgUf
5FElkd2quTIDdcH Dt7Rz5X/H7DaFYrocp4XSVj tgs1JVPA7s8cK/3N+PiFEwSrrU
97lb31A/0ZlebgWlZ5N0BlB 8Z9RrLm21AqugAN TVnbSSmcL9DKA9E 5NWSh5Lzk7W
qgCipji3y23jQX+ +sF1Yl496vr6ShT tnkx/dLI5wtIev97tmJI B2oXrikC28vjyn
i3fDZ2o6cK803iP 7tlzbrxcKxMt8cr tI2BS0nQxe1M/Op5p1l9vvaSbnKq 8TFkJt
4ZemFvw64bHB6wG CHuyPoiG8itPVpH GH1IgL7bPWWpjT1 R0Yrn9ACQgmKcAE rzcU
xOhCowEqYNBu8RE 05j6NK+cKcQ1/BAhZgNVi174WfQ2 r0fPm0wOzSrBWLy HGDBvv
5awsrhKhrpm4Fji ZZPmNyUQWpX3a9U oauU4Br3HOfTCWf mN+JwsO8mlI6DNz 5qcg
AS+0YGCHahUYw5h yw7CpM3J0r7pD8C ZP7v/uiH+KU1V74iAvxf jGe2qT/AQIy/df
hr61ZhxiivkOkWV F2TXULk7N0gWWfz ROOZWiw+2D8yHqm CBR8a/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

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

Similar topics

4
2490
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 used in hardware programming. Are they used very often in routine C/C++ programming, and what for? thanks, MJH
6
8984
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 programming in straight C, the Win32 API sends windows messages to applications with various settings stored in bit fields, in which case changing the settings of the message is a matter of using the bitwise operators. This was a good practical example of...
2
3469
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 with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
9
7030
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 from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8. To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe...
5
5804
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 you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...
5
3315
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 OR) ~x Bitwise negation
2
3832
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 read from buffer is defined similarly. So basically, I want to read sequence of bits from buffer at specified bit-location. Further, I want to do this efficiently. For example, start index is 13. The length of sequence of bits to be read is 17....
45
5313
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 multiple it by 1000 to get 11111 and the preform bitwise like <<2 to get 88888 and then divide by 1000 to go back to float 8.8888. but these seem like "nasty" way to do it. So maybe some of you have great tips? Thank you in advance! L R
29
5958
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 often done in Python because of keyword arguments and dicts, which are lot more versatile. Another major use, talking to hardware, is not something oft done in Python either. It seems like this occasional usage wouldn't justify having built-in...
0
9565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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 we have to send another system
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.