Lab materials
- Slides: Structures
- Additional example: structure array (includes passing struct arrays to functions)
In the lab, a sample solution will be written together and commented where data fill be read from the file and stored in a struct array. This is also published as a code example for those not participating in the lab: Reading from a file into structure array
Lab tasks
In this lab, you have one task which is expanded by 4 advanced tasks.
Lab task: employee search
In this program, we will mimic an employee database from where we will filter out employees based on search criteria.
Data file
Download the test data from the following link https://blue.pri.ee/ttu/files/iax0584/andmefailid/3_data_short.zip
Data file is composed of randomly generated data.
Requirements
- Read employee data from the file
- File has one record per line
- Every data field is separated by a space. All data fields are single word.
- Data structure in the file:
<eID> <first_name> <last_name> <city>
- You need to keep the data in the memory as a struct array
- Program must work in a situation where the exact number of records is unknown and can change (e.g. somebody gets employed, fired or leaves)
- Create a reasonable limit in your program and test that you wouldn’t go beyond it
- If the data file is longer than the allowed limit, print a corresponding message. Behavior after this is up to you.
- Allow the operator to perform searches based on the city name
- Program will output all employees in the city
- Show how many matches were found (E.g. [0 / 91])
- Search must be repeatable without exiting and restarting the program
- In the base task, print all the data available – eID, last name, first name, city
- User must be able to exit the program when they desire. Create functionality for that (e.g. a specific command)
- Reading the data file more than once within your program is not permitted.
- The structure members don’t need to be an exact match for the file. You can add additional struct members – this is useful for the advanced tasks.
Example output
NB! The example output already includes all 4 advanced tasks!
Advanced task 1: Sorting structures
Requirements:
- The list of employees must be sorted alphabetically by the last name in ascending order.
- Output name format must be last name, first name
Advanced task 2: List available cities
Requirements
- List all the cities present in the file
- List of the cities must be generated based on the data file (must not be hardcoded into the program)
- Operator must be able to print the list of city whenever they wish by typing the appropriate command. Add a command for this to your program.
- Operator must be able to repeatedly print out the list during the runtime of the program.
- The list of the cities can only be composed once per execution of the program.
Advanced task 3: Better search
Requirements
- The search must be case-insensitive. Searching for either TALLINN or tallinn must print all employees living in Tallinn.
- Search must support partial matches. E.g. when searching for all , you should display employees living both in Kallaste and Tallinn.
- Both features must working at the same time. The last result should also be obtainable by entering ALL or aLL
Advanced task 4: Parse the eID
Requirements
- Do not display the eID in the output of your program
- Display the employees date of birth with the format d. MMMM yyyy
Official information about eID: https://www.riigiteataja.ee/akt/114022017005
Short description of it in English: https://en.wikipedia.org/wiki/National_identification_number#Estonia
After the class, you should
- Know how to declare a new structure type
- Know how to pick members for a structure
- Know what is structure padding and how it affects the size of the structure
- Know what affects the final size of a structure
- Know which structure member access exist and be able to use the dot operator
- Know how to create an array of structures and read data from a file into that array of structures
- Know how to assign structures
- Know how to give existing types new names (use typedef)
Additional content
- Structures in C
https://www.geeksforgeeks.org/structures-c/ - Structure Member Alignment, Padding and Data Packing
https://www.geeksforgeeks.org/structure-member-alignment-padding-and-data-packing/ - Typedef in C
https://www.geeksforgeeks.org/typedef-in-c/ - Why to avoid typedefs with structs (by Linus Torvalds, author of Linux Kernel and Git)
https://yarchive.net/comp/linux/typedefs.html - Structure padding in C
https://www.scaler.com/topics/structure-padding-in-c/ - C structures
https://www.tutorialspoint.com/cprogramming/c_structures.htm
- Comprehensive list of date and time formats used in .Net.
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings