CIS 1057. Computer Programming in C

Chapter 11. File Processing

 

1. Input/Output files

C can process text files and binary files. A text file is a sequence of characters; a binary file is a sequence of bits.

A file ends with a special end-of-file character, or <eof>. Reading it will fail, and returns an EOF value (a negative integer).

End of line is represented by a special character '\n', which is processed normally. It is an "escape sequence" that start with '\'.

A data source or destination is often called an input or output stream. Files, monitors, and keyboards can be seen as streams.

System file pointers: stdin for keyboard input, stdout for normal output to and monitor, stderr for error message.

Place holders are used in input/output formats.

Files are decleared as pointers of FILE. An "empty" pointer has value NULL.

Example FIGURE 11.1: backup a text file.

 

2. Binary files

A binary file is more efficient, but not human-readable. It is often system-dependent, too.

Example: FIGURE 11.3, which uses function fwrite to write into a binary file. The corresponding input function is fread.

 

3. Database search

Example: FIGURE 11.5 and FIGURE 11.8.