jmoy said:
I have some data (say in a file) that needs to be handled byte by
byte. Source code I have looked at does this by treating the data as a
stream of 'char's. However, the standard does not require a 'char' to
be a byte wide. Stroustrup's TC++PL mentions an implementation where a
'char' is four bytes. How can I write my program so that it will work
even with such an implementation?
In modern times, few devices actually require byte by byte handling.
Most of them are designed for speed, and that speed is most efficient
handling "streams" of bytes. For example, many files like to transfer
sectors at a time. So one would allocate an array the size of a sector,
read the sector from the file, then parse through the array.
For text files, especially ones that have records delineated by a
newline, are best read line by line into a std::string. This is still
not processing the file byte by byte.
Also, you will have to separate in your mind, the concept between a
byte, octet and a character. A byte is the minimal unit in computing;
it can be 8 or more bits. The bits in a byte need not be a multiple or
power of 8. An octect is a unit of 8 bits. A character is a single
textual unit, often times a letter. The character may be as small as
6 bits or higher; the number of bits used depends on the platforms
character encoding scheme. The CDC Cyber computers have a 6 / 12 bit
character (popular letters take 6 bits, less popular require 12 bits).
Some Asian character sets require 16 or more bits. Just remember that
there is a difference between a byte, octet and char.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book