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 three extra tasks.
Lab task [W03-1]: employee search
The purpose of this task is to introduce you to using structures. The task will mimic a very basic employee database where the user can search for employees and that also supports commands.
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
- Reading the data file more than once within your program is not permitted.
- 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>
- The data needs to be kept 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
- If the data file is longer than the allowed limit, print a corresponding message. Behavior after this is up to you.
- The program must allow for repeated searches and processing of commands without restarting the application
- 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
- There must be support for commands
- Commands should start with a “:” to identify them from city searches
- Command “:all” must print all people in the database, without any filtering
- Command “:exit” must close the program
Hints
The command parser sounds “scary”, while the solution is extremely simple. All of this you learned in Programming 1 in the strings class. This is a 3-step process
- Identify that user entered a command (check that the 0-th character is ‘:’) – this was an example on the slide
- Make a pointer to the second character ( char *command = input + 1; )- this is pointer arithmetic from week 1, also used in week 2 for the file extension
- Compare the command to known commands
Example output
The first example is about command handling. If it cannot recognize a command, it states so. If a command is entered without a colon, it will search instead (default behavior).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
risto@risto-lt:~/pr2/wk3_struct1$ ./task_basic 03_data_short.txt Available commands :exit | close the program :all | print everyone in the database Enter a city name to search or a command > :add Error! Unknown command! Available commands :exit | close the program :all | print everyone in the database Enter a city name to search or a command > exit Searching for: exit Employees listed [0/150] Available commands :exit | close the program :all | print everyone in the database Enter a city name to search or a command > :exit risto@risto-lt:~/pr2/wk3_struct1$ ./task_basic 03_data_short.txt |
The second example shows how searching is handled.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
risto@risto-lt:~/pr2/wk3_struct1$ ./task_basic 03_data_short.txt Available commands :exit | close the program :all | print everyone in the database Enter a city name to search or a command > Sindi Searching for: Sindi Saar, Anne City: Sindi eID: 49112012007 Mitt, Keit City: Sindi eID: 48012281578 Kallas, Ivari City: Sindi eID: 50107042125 Employees listed [3/150] Available commands :exit | close the program :all | print everyone in the database Enter a city name to search or a command > :exit |
Note that the output of “:list” command is not shown due to it being somewhat long.
The last example shows the program when all extra tasks have been completed. This is wrapped in a spoiler to reduce the size on normal vieweing.
Extra task 1 [W03-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 (cannot be hardcoded into the program)
- List of cities must be ordered alphabetically
- Add a command “:list” to your command parser.
- 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
Extra task 2 [W03-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.
NB! Notice that :all is a command printing everyone and all is a search phrase printing people in Tallinn and Kallaste.
Extra task 3 [W03-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