COMP 166 - Lab 5 - File I/O

Learning Outcomes and Introduction

In this lab assignment you will learn about:

You will also practice using functions and loops, and translating mathematical expressions into code.

Task 1: Echoing Files

Create a program that prints the contents of input files to standard output.

Your program should accept a list of input file names to read as command-line arguments. If a file name is preceded by a "-a" switch, the contents of the file should be interpretted as a list of additional file names to open. Otherwise, each named file should be read and printed in the order given.

Example usage
./task1
Print a usage summary, such as "usage: ./task1 [-a] [file ...]"
./task1 file1.txt file2.txt
Print the contents of file1.txt, followed by the contents of file2.txt
./task1 -a filelist.txt
Read a list of input file names from the content of filelist.txt and print the contents of each file in order.
./task1 file1.txt -a filelist.txt file2.txt
Print the contents of file1.txt, followed by the contents of each file listed inside filelist.txt (in order), followed by the contents of file2.txt.

If any input file cannot be opened, or an error occurs during reading, print an error message to the standard error output and continue to the next input file.

Task 2: Formatted Input

Create a program that calculates statistics from a log of sensor data and prints a report to a file.

Your program should accept two command-line arguments: the name of the log file to read, followed by the name of the report file to write. If only one command-line argument is given, write the report to standard output instead. If no arguments are given, read the sensor data from standard input instead.

The log data your program processes will be formatted as a table of values. Each line of input will begin with a timestamp, followed by a list of floating-point sensor reading values (separated by white-space). The number of sensor readings on a line can vary from file to file, but every line in a file should have the same number of readings. Every line of input is guaranteed to be less than 500 characters long.

If the log data file cannot be opened, or an error occurs during reading, print an error message to the standard error stream and terminate the program. Similarly print an error message and exit if the log file contains any improperly formatted data or has an inconsistent number of sensor readings on each line.

The report generated by your program should display the timestamp that the minimum and maximum sensor values were recorded (across all readings). In addition, it should report the statistical mean (average) and standard deviation of the values in each column of sensor data.

Example input and output

Given an input file like:

Download example-input.txt

Your program should output something like:

Download example-output.txt
Math details

The statistical mean for a set of values is given by the formula:

= Σxi N

Where xi is a single data value (such that Σxi is the sum of all values), and N is the total number of values.

The standard deviation σ for a set of values is usually defined as:

σ2 = Σ(xi - x̄)2 N - 1

However, this formulation would require two passes over the data set in order to first compute the mean and then compute the deviation using the mean. An alternate form can be used to compute the standard deviation in a single pass:

σ2 = N(Σxi2) - (Σxi)2 N(N - 1)

Submission

Completing all tasks in this lab should result in 2 or more separate files within a single folder named like LastnameFirstname_lab5. Compress the folder into a ZIP archive and upload it to the D2L assignment.

The marks for this lab are heavily weighted towards good coding practice and style. Pay attention to your code formatting, use comments to clarify your code, and make sure you are using defines, arrays, loops, and functions effectively.

NOTE: This assignment is to be done individually. You can help one another with problems and questions, but in the end everyone must write and submit their own code.

CriteriaMarks
Programs compile and run without error 3
Good coding style 3
Task requirements met or exceeded 3
Learning Outcomes achieved 3
Total 12