Skip to main content
Logo image

Dive Into Systems: Exercises

Section 2.8 Input/Output in C

Checkpoint 2.8.1. Format descriptors.

Checkpoint 2.8.2. Parsons Problem: File I/O.

Put the following lines of code in order to read a single integer value into the variable named population from the file demographics.txt. If the file cannot be opened, the code should print "Error opening file" and then quit with a call to exit().

Checkpoint 2.8.3. Parsons Problem: File I/O (2).

Write a program that opens a file named "hamlet.txt", and counts how many times the letter h appears in the file (both in lowercase and in uppercase), as well as how many total characters are in the file. The program should then output the following:
  • The total number of ‘h’ or ‘H’ characters that appear in the file.
  • The total number of characters in the file.
  • The percentage of characters in the file are h or H.
Sample output (for the actual play Hamlet):
Count: 8688
Total Count: 182567
Percent H’s: 4.76