Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Binary reading
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Ktomislav




PostPosted: Sat Feb 14, 2009 12:41 pm   Post subject: Binary reading

Is it possible to read file bit by bit to boolean?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Feb 14, 2009 1:19 pm   Post subject: RE:Binary reading

You can read in individual bytes, then use bit-wise operators to access the individual bits.
Ktomislav




PostPosted: Sat Feb 14, 2009 3:15 pm   Post subject: Re: Binary reading

Can I do it like this?
code:
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




PostPosted: Sun Feb 15, 2009 12:05 am   Post subject: 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
code:

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




PostPosted: Sun Feb 15, 2009 8:28 am   Post subject: Re: RE:Binary reading

btiffin @ 15.2.2009, 06:05 wrote:

code:

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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: