
-----------------------------------
Ktomislav
Sat Feb 14, 2009 12:41 pm

Binary reading
-----------------------------------
Is it possible to read file bit by bit to boolean?

-----------------------------------
wtd
Sat Feb 14, 2009 1:19 pm

RE:Binary reading
-----------------------------------
You can read in individual bytes, then use bit-wise operators to access the individual bits.

-----------------------------------
Ktomislav
Sat Feb 14, 2009 3:15 pm

Re: Binary reading
-----------------------------------
Can I do it like this?
ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
file.seekg (ios::beg);
char byte[1];
file.read (byte, 1);


And could you please explain me why do I have to use ios::ate and  ios::beg instead of nothing or just  ios::beg?

-----------------------------------
btiffin
Sun Feb 15, 2009 12:05 am

RE:Binary reading
-----------------------------------
ios::ate will seek to end on open, so then ios::beg will be required to seek back to the beginning.  If you don't set ate, you won't need the seek to beginning.  If you have something like

char charvar[1];
ifstream myfile;
myfile.open("filename", ios::in | ios::bin);
file.read(charvar, 1); 

should work.

The fine control provided by the I/O stream flags and manipulators requires a fair level of understanding for optimal use.

http://www.cplusplus.com/doc/tutorial/files.html is a good read.

Cheers
Edit; yeah, more typos

-----------------------------------
Ktomislav
Sun Feb 15, 2009 8:28 am

Re: RE:Binary reading
-----------------------------------


char charvar[1];
ifstream myfile;
myfile.open("filename", ios::in | ios::bin);
file.read(charvar, 1); 

should work.


I know, but it doesn't work. Never mind, I figured out what I needed and thank you for replying.
