|
Text and Binary |
Top Previous Next |
|
The basic units in the discussion of stream I/O thus far have been characters. A character is a value of type char or unsigned char. On the C6000 this is held in an octet (or 8-bit byte), which is the smallest addressable unit of memory. Both in the C6000's memory and in a file, one character is held in one octet. Larger values than those that can be held in a single octet are stored in files as a sequence of octets. Thus, a value of type int is held as a sequence of four octets. By convention, these are stored in little-endian order; that is, the octet holding the low-order eight bits of the value is stored in the earliest position in the file, and the octet holding the high-order eight bits in the latest.
Characters may be stored in files as text or binary data. The difference is that text files are organized into lines. From the point of view of the program, the newline character, '\n', separates lines. The program can end a line by outputting a newline character, and on input, the end of a line can be found by comparing the characters being read with the value '\n'. Under MS-DOS, these newline characters are inconveniently stored in files as carriage-return line-feed sequences; the conversion between newline and carriage-return line-feed is performed by the server, and is usually invisible to the program.
Binary files are not divided into lines, and each character is read or written 'as is', without conversion.
By default, Diamond reads and writes text files. If you need to process binary data without conversion, you must inform the run-time library that a particular file is to be processed as a binary file. You can do this by using the 'binary' specifier "b" in a call to fopen. For example:
fd = fopen("x.bin", "rb");
Notice that the C6000 Diamond implementation of stream I/O is rather different from the implementations on some other processors, notably the TMS320C40, where the smallest addressable unit of memory is a 32-bit word. In particular, these processors require a distinction between packed and unpacked files, which is unnecessary and not implemented on the C6000.
|