Managing multiple files

This sample program reads data from a file line-by-line and if whenever it finds students who got a 5, it will write it to a second file.

The format for the input file: <name> <grade>

Notice the following:

  • The second file is not opened before we make sure that the first one opened successfully
  • If opening the output file fails, first the input file is closed and only then we exit the program.
  • Notice that this program is also susceptible to buffer overflow attacks (not the scope for this sample).