PR1EN15: Files

Lab materials

Lab tasks

This lab has two tasks, both of which have advanced tasks building upon the base task.

Task 1: Odd or even

The main purpose of this task is to practice having multiple files open at the same time while getting all the technical nuances correct.

Requirements
  • Read an unknown number of integers from a file. Numbers are separated either by a space or a newline. There can be infinitely many numbers.
    • Example: 5 3 -6 0 25 955 -1024
  • The numbers will be divided into files based on the following principles:
    • 0 or lower numbers are ignored.
    • Positive odd numbers are written to the file odd.txt .
    • Positive even numbers are written to the file even.txt .
  • Follow the basic best practices for working with files
    • Opening of the file is always checked.
    • Notify the user if the file doesn’t open, including which file it was.
    • Close the file before exiting.
  • If the input file is corrupted, the program must halt upon detecting it
    • E.g. 9 -5 hey 14
Warnings and hints!
  • Be very careful with infinite loops when writing to files. You can fill up the drive space in just a few seconds
  • Write the output on the screen during testing so you can follow along what’s happening and to which file data is written to. This way it is easier to detect if something goes wrong and terminate the program ( ctrl+c )
  • If the first file opened successfully, but the second one didn’t, you must close the first one!
Advanced task 1: Statistics

Find the following statistics

  • Sum and arithmetic mean.
  • Smallest and greatest number.
  • The results must cover all the values from the input file regardless of the value (this includes negative numbers).
  • You’re not allowed to read the file more than once.
  • Program must still work with infinitely many numbers.
Advanced task 2: command line arguments

Add support for command line arguments. Change your program as follows

  • The name of the input file is always read from the command line
    • If the name of the file was not given, give an error and exit
    • The name of the file must always be the first argument
  • Verbose
    • Your program must detect if the argument -v  (verbose) was passed to it.
    • This is a common argument to make the programs chatty (simplifies detecting errors).
    • Program will display when and which input files were open (including the name of the file).
    • Program will display all numbers read from the input.
    • Program will display (if) the value was written to an output file, including which file, or discarded.
    • Program will display closure of each file
    • If verbose is not active, the program should not print any lines in the output (terminal), with the exception of error messages and advanced task 1.

Both of these arguments are widely known and used practices for command line programs, but are are also often available for graphical programs.

Examples of how the program should be executed:

  • ./parity input_nums.txt
  • ./parity input_nums.txt -v

Task 2: Processing penalty notices

Your task is to create a program that can compose penalty notices from speed camera measurement data.

Additional references

The task is composed based on the following guidelines

Principles of penalties
  • The measurement uncertainty is 4 km/h (50 – 90 km/h speed limit area)
  • The processing of the penalty is started if the vehicle exceeded the speed limit by at least 3 km/h.
  • Every km/h that is exceeded from the limit is fined by 5€.
  • Maximum penalty is 300€.
  • If the speed limit was exceeded by more than 50 km/h, a general procedure is started.
  • E.g. If a vehicle is driving 99 km/h and the speed limit is set at 90 km/h, the fine is going to be 25€.
Input file

Download your input file from here: 14_2_speeds.txt

Input file contains one measurement per line. The structure of the input file is: <registration number> <measured speed> <speed limit>

  • Car registration number – up to 9 characters
  • Measured speed – positive integer
  • Speed limit – positive integer
Output files

Your program must create two output files – one with processed fines and another for general procedure calls.

First file will contain fines. This file will only contain  fine notices that the owners of the respective vehicles need to pay. Each fine notice will be written on a separate line. Every line has three data fields:

  • Car registration number
  • Exceeded speed without the measurement uncertainty (how many km/h in excess was measured)
  • Penalty (amount to be paid)

Expected output:

The contents of the second file are notices for general procedure. You can only write those entries against whom the general procedure will be started. The format for those notices is as follows:

Expected output:

Advanced task: Settings

Due to regulations and laws changing, it must be possible to easily adjust the settings that the application is using. Figure out a way to implement it so that the following requirements would be satisfied.

1. settings for processing of notices must be configurable:

  • Fine size for every exceeded km/h (e.g. 3€, 5€, 10€)
  • Maximum fine size (e.g. 100€, 190€, 300€)
  • Speed excess to start the general procedure (E.g. 40 km/h, 61 km/h, 100 km/h)

2. adjusting settings must be simple but stay invisible during day-to-day operations

  • Adding the ability to adjust settings must not add extra time to use the program normally.
    Thought: The officials working with the application on a daily basis don’t want to waste time on adjusting settings or even confirming current settings each time they use it.
  • Changes must persistent – once settings are changed, updated values should be used until they are changed again
    Thought: Values will typically be updated when the laws are changed. It is done quite seldom, so the operator doesn’t want to reconfigure it on a daily basis.
  • Changing the setting must not require recompilation of the program.
    Thought: You wouldn’t expect officials to know how to edit source code or recompile a program. Even if done by the IT, they are just support, not developers.

After this class, you should

  • know how to open and close files.
  • know how to check if a file opened.
  • know the difference between file modes and how they behave if the file exists and if it does not.
  • know in which situations files may fail to open when opened for reading or for writing.
  • know different ways of addressing files.
  • know why buffering files is important and what problems it may cause.
  • know how to read and write files.

Additional content