C Interview Questions and Answers


What is the difference between text and binary I/O?

In text mode, a file is assumed to consist of lines of printable characters (perhaps
including tabs). The routines in the stdio library (getc, putc, and all the rest)
translate between the underlying system's end-of-line representation and the
single n used in C programs. C programs which simply read and write text therefore
don't have to worry about the underlying system's newline conventions:
when a C program writes a 'n', the stdio library writes the appropriate
end-of-line indication, and when the stdio library detects an end-of-line while
reading, it returns a single 'n' to the calling program.

In binary mode, on the other hand, bytes are read and written between the program
and the file without any interpretation. (On MS-DOS systems, binary mode also turns
off testing for control-Z as an in-band end-of-file character.)

Text mode translations also affect the apparent size of a file as it's read.
Because the characters read from and written to a file in text mode do not necessarily
match the characters stored in the file exactly, the size of the file on disk may
not always match the number of characters which can be read from it. Furthermore,
for analogous reasons, the fseek and ftell functions do not necessarily deal in
pure byte offsets from the beginning of the file.

Posted by:Richards