ABSTRACT

We have already taken advantage of redirection to use files as inputs and outputs. This chapter explains how to use C functions to read from or to write to files without using redirection.

A program has many ways to obtain input data, for example: • Using scanf to get data from a user through the keyboard. • Using scanf and redirection to get data from a file. • Using argc and argv to get data from the command line. • Using file operations to get data stored on a disk. For the last option, the program must first obtain the file’s name. The file’s name is itself

a piece of data. The example below uses argv[1] as a file’s name. The program must check whether argc is at least two to decide whether argv[1] can be used without generating a memory error.