Skip to main content

Large data sets cannot be repeatedly interactively entered as that would take far too long. Imagine if the bank had to hire people to type out every account number for every customer each month in order to update the balance in each account... this is not a trivial task. Thus, when the data gets large, the data ends up being stored in some sort of permanent storage. For large applications (like that of a bank) this would typically be in the form of data bases. However, for smaller applications, we may end up storing the information in to a file.

We have seen files that hold information already.. our C programs are all stored in files. When a compiler translates the C code into machine language, it reads the data (the programs) written to the files and translates it into machine code which is then stored into an executable.

In this chapter, we will look at how to create, read and alter the content of files from our programs.

Learning Outcomes

After completing this chapter, students will be able to:

  1. Declare and use file pointers to create handles for file operations using FILE* variables and understand the role of file pointers in accessing files
  2. Open files using fopen() with appropriate access modes ("r", "w", "a") and implement proper error checking to verify successful file opening
  3. Read data from files using fgets() and fscanf() with format strings and regular expressions to extract structured and unstructured data
  4. Write data to files using fprintf() to send formatted output to files in write or append mode
  5. Close files using fclose() to ensure data is properly flushed and system resources are released
  6. Implement common file I/O patterns such as reading files line-by-line, character-by-character, and reading structured data records
  7. Use regular expressions in fscanf() to parse formatted data files and extract specific fields from structured records