Lab 2. Simple file processing using redirection

How to submit your code

Each program (the source code .cpp file) should be submitted through Blackboard (Course Materials > Lab).

You can submit all your programs at the end of the lab session in one submission. This way, we can hopefully avoid the situation when you are quickly writing your program, immediately uploading it to Blackboard, but then, say 10 minutes later, realizing that there is a bug in it.

Basically, submit when you are sure that it will be your final version.

Each program should start with a comment that contains your name and a short program description, for example:

  Author: Your Name 
  Description: Lab 1. Task 1. Hello world

You can submit incomplete programs, but their “incomplete” status must be clearly mentioned in the comment to the program. In this case, also briefly describe what is implemented, and what is not.

Lab 2. Simple file processing using redirection

Reading user’s input from the keyboard is a great feature, however it’s not the only possible source of input for a computer program.

When a lot of data must be processed, you can store this data in a file, which in turn can be read by your program.

The textbook describes file streams in the section 2.4. However, there is an even easier way to read a file, and we are going to use it today. It’s called standard input redirection.

stdin redirection

So if we run the program as follows:

$ ./program < file.dat

the contents of the file “file.dat” will be sent to the standard input of our program, so we will be able to read all of it as if it was input from the keyboard (in C++, we usestd::cin in this case).

How to identify the end of the file? std::cin >> x will return a fail-state that works basically like false, once you reach the end of file. Thus in C++, an easy method to read the entire file from the standard input, is the following while loop:

  while (cin >> x) {

In this lab, we are going to read numbers (positive integers) from the file. So, the program that reads integers from the file and prints them on the screen (each on its own line) would look like this:

  int n;
  while (cin >> n) {
    cout << n << endl;

Example input data files:

10.dat, 20.dat, 30.dat, 40.dat, 50.dat, 60.dat, 70.dat, 80.dat, 1k.dat, 10k.dat, 100k.dat. (Also, feel free to write your own input files to test your code)

Task 1. Maximum value

The program should read the file redirected to standard input. The file contains positive integers.

After reading the file, the program should print out the largest integer found in the file. (Hint: In your program, use a variable to keep track of the largest integer so far)

Task 2. Maximum difference

The same as the previous task, but the program should report the largest absolute value of the difference between two consecutive integers in the file.

Basically, you have to find the largest jump (up or down) in the sequence of the integers read from the file and print it.

Example, for the input:


The output sould be 80, the largest difference is |97 - 17| = 80.

Task 3. Printing a bar chart

Read the integers from the file, and print a bar chart, such that each integer N read from the file is show as a group of N vertical bar symbols |.

Example, for the input:


The output should be:


Task 4. Bonus.

If you still have time, write a program that generates your own data files.

Using standard output redirection, instead of printing on the screen, you can print in a file:

$ ./program > outputfile.dat

You may use, for example, <cmath> functions and pseudo-random number generation functions rand and srand.

In the program, write a comment explaining what your program is generating, are the numbers random or generated using mathematical functions? What kind of result are you trying to achieve (if any)?

Data files format:

I provide some data files for you to test your programs. The format is simple, it’s a plain text file with one positive integer per line, for example: