In K & R ANSI C book(2nd Edition), in page 241, the following lines
are mentioned.
"The library supports text streams and binary streams, although on
some systems, notably UNIX, these are identical."
Here I am unable to understand what is meant by saying "the streams
are identical" ?
Kindly clarify. If possible give an example so that I can understand.
Binary streams are simply read and written byte by byte. Text streams
are allowed to be processed in a more complicated fashion. To take the
simplest example, on DOS/Windows systems, writing a single '\n'
character causes two bytes to be written to the output file, "\r\n" or
"\n\r", I can't remember which. Other systems have the opposite order.
Some systems store text streams in fixed-sized blocks, keeping each line
of text in a separate block, padding it with null characters or blanks
to the end of the block, or storing a count in each block of how many
bytes of the block are currently in use.
If you open the file for writing in text mode; if you never write
anything to it other than printing characters (see isprint()), '\t', and
'\n'; if you never write a space character prior to a '\n'; if you
always end a file with a '\n' character, then you when you close the
file and reopen it for reading in text mode, you will get back exactly
the same text you wrote. This is all taken care of invisibly. Note: it's
implementation defined whether or not spaces printed out before a '\n'
will appear when you read back the line.
However, in every case where the standard allows for more complicated
handling for text streams, simply reading and writing a single byte at a
time in sequential order is always an option. On some systems (most
notably, on Unix and Unix-like systems), that is precisely what is done.
The result is that it makes no difference whether you open the file in
text mode or binary mode. If you write a '\n' character to a file, a
single byte with a value of '\n' is written to the file.