7. Implementing the date verification algorithm

Generic information and test code7th week lab taskDifferences in Win10 build 14986 and beyond

The data to work with can be provided by many means:

  • Keyboard
  • Generating random results
  • Initializing
  • From files
  • From databases
  • From sensors
  • etc.

So far we’ve used keyboard to enter our data. Our next step would be to become closer with files and for now, we’ll use an intermediate step before actually handling files with our code. To do that, we’ll redirect the input stream that usually comes from keyboard to read data from a file. This is done on the operating system level.

To understand this better, we need to understand the standard streams and how they work. We have input stream stdin, which typically comes from the keyboard. Then we have two output streams stdout and stderr, which typically print to the terminal window, but can be separately manipulated. Stdout is our standard output stream and stderr stream is used for error messages.

Trying it out yourself

To achieve this, we’ll need 2 files. For the sake of simplicity, we should put them in the same folder.

  • The program (NB! Source code is not a program! A program is a binary file that we get from compiling the source code).
  • Data file (text file that contains data we would have otherwise typed on the keyboard)

When creating the data file, you should consider covering the most amount of different data that would test your program to the fullest. Always try to think about edge cases. It might be needed to have multiple test files.

The following example goes together with the lab task given last week (matrices).

Compared to the lab task on week 6, where the data was initialized, now we’ll try to use streams to get this input data to our program. If you already have the code from last week, you only to remove the initialization of the array and add the nested for loops required to read them from the keyboard. If you don’t have this code, you can use the following code to test this.

As you can see from the code, we don’t need to change anything compared to normally reading the input values from the keyboard. Everything is done on the operating system level, so it’s quite easy to test this and other code you have with user input.


Now we need to compile this program. You can do it using any means already familiar to you. However it is important that you don’t forget to compile after every modification. This is something often forgotten when the program is run from the command line.

Runnig the program

To run this in windows, you should navigate your file explorer to the desired folder (where the program and data file are stored). Now hold down the shift key and right-click with the mouse on an empty area (not on any of the files).  Select the option “Open command window here”.  As an alternative, you can open the command window anywhere and navigate to the desired folder.

Now to run the program and redirect the input, you need to specify the name of the program, then add the symbol ‘<‘ to redirect the stream and then enter the name of the data file.

E.g. 7_base_read__matrix.exe < input.txt

In linux it works out almost identically. You need the terminal window to be in the right folder – you can open it with the right click and if necessary, move with the cd command. The difference from Windows is that Linux doesn’t really care about extensions for the binary or text files. There is a special execute flag for programs and all others are considered plain text files.

E.g. ./7_base_read__matrix < input

Additional reading:

Task: create a program that can verify correct dates and give an error if the the date is invalid.

Requirements for base task

  • After each date, it should specify if it was valid or not
  • Valid years: 1900 – 2099
  • Leap year check
  • Must be able to verify 100 dates ina single run
  • The test data comes from the input stream which has been redirected to take it from a file
  • Date format: DDMMYYYY


  • Give a reason why the date failed verification (e.g. too many days for leap year February, year out of range)
  • Unlimited amount of dates
  • Don’t assume that the input is given correctly – user might input characters, wrong amount of numbers etc.
  • Allow at least two formats
    e.g. [DDMMYYYY], [DD.MM.YYYY], [mm/dd/yyyy], [dd. mmm yyyy]


Steps needed

To start off, we should create a text file that will contain the data we would have otherwise typed in from the keyboard. When writing the data file we should make sure that all of the different cases would be covered that may occur. This will make it easier to test our program.

The data file could look like this (don’t be afraid to add lines or write Your own version)

Now that we have the data to test, we need a program that would test it.

After we have all the files, we need to compile it. You can use any of the methods You’re familiar with by now.

Now when we have the program ready, we should get out our command prompt and divert it to the folder that we stored the files in.

To verify the contents You can use:

In this folder I have 3 files:

  • kp – the so called binary file a.k.a. the program that I can execute (the file I got from compiling my source code)
  • kp.c – the source code I used to compile my executable
  • input – text file, that contains my test data.

Now we run the program so that instead of writing the input from the keyboard, it will be read from the file that we’ve composed (using the stream redirection). For this, use the following line: ./programname < datafile

./programname – this is how you can run a program in linux console
< – a symbol that will redirect my input stream from keyboard to a file specified after it
datafile – the redirected input will be read from this file instead of the keyboard

After implementing the date verification, Your output might look something like this:

If You’re going for advanced implementation, try validating the input before checking

Starting from Windows 10 build version 14986 they have replaced Command Prompt with PowerShell in the context menu. This page has some information about the changes and how to cope with them. I’m offering two solutions, one with using PowerShell and the other with using Command Prompt.

Using Command Prompt

Even though you can’t open CMD window from the context menu (unless you change the behavior of your OS permanently), it can still be opened. There are two possible ways to open it.

a) In the start menu, look for Command Prompt and run it (keywords can either be “command prompt” or “cmd”

b) Run command prompt from the run dialogue. To open run either right click on the start menu and select run or use the keyboard shortcut Win+R

Now that you have the command prompt open, you need to navigate to the correct folder. To do this, use “cd” to change directory.

Command Explanation
cd .. Go to parent directory
cd folder Go to a subdirectory
cd C:\folder\subfolder\ Go to a location specified by the full path
F: Change drive

Having navigated to the correct folder, the rest is identical with the guides already provided. To assist you further, now follow three different figures you help you navigate through your folder structure.

NB! The following pictures also include the compilation process. If you do it in Geany, this doesn’t need to be done. Also, it might be advisable to add additional flags (e.g. -Wall, -Wconversion) which isn’t shown in the figures.

Navigating using the full path
Navigating using relative adressing
Changing to a different drive / network resource


Using powershell

If you wish to use PowerShell, You can navigate to the desired folder (using windows explorer or any other file explorer) and open it with the path already set. After navigating, hold down the shift key and press right mouse button to open the context menu. From there open the PowerShell window.

Now that PowerShell window opened in the desired location, you can immediately start running your program. However it should be noted that PowerShell does not support redirecting the input stream. To circumvent that, we can use what’s known as a pipe and send the output of the program Get-Content to our program as input.

Run: GetContent data_file.txt | program.exe