{"id":6537,"date":"2021-12-08T21:36:58","date_gmt":"2021-12-08T19:36:58","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?p=6537"},"modified":"2025-12-07T13:51:38","modified_gmt":"2025-12-07T11:51:38","slug":"pr1en15-files","status":"publish","type":"post","link":"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/","title":{"rendered":"PR1EN15: Files"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Lab_materials\" >Lab materials<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Lab_tasks\" >Lab tasks<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Task_1_W15-1_Odd_or_even\" >Task 1 [W15-1]: Odd or even<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Requirements\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Warnings_and_hints\" >Warnings and hints!<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Extra_task_1_W15-3_Statistics\" >Extra task 1 [W15-3]: Statistics<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Task_2_W15-2_Processing_penalty_notices\" >Task 2 [W15-2]: Processing penalty notices<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Additional_references\" >Additional references<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Principles_of_composing_penalty_notices\" >Principles of composing penalty notices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Input_file\" >Input file<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Output_files\" >Output files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Extra_task_W15-4_Settings\" >Extra task [W15-4]: Settings<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#After_this_class_you_should\" >After this class, you should<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/#Additional_content\" >Additional content<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Lab_materials\"><\/span><strong>Lab materials<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Slides: <a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/slaidid-en\/15_files.pdf\"><strong>Files<\/strong><\/a><\/li>\n<li>Example:<strong> <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/grades-c\/\">Managing multiple files<\/a><\/strong><\/li>\n<li>Example: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/file-name-as-command-line-argument\/\">File name as a command line argument<\/a><\/strong><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Lab_tasks\"><\/span>Lab tasks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This lab has two tasks, both of which have an extra task extending the base features.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Task_1_W15-1_Odd_or_even\"><\/span>Task 1 [W15-1]: Odd or even<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The purpose of this task is to practice using multiple files at the same time while getting all the technical nuances correct. It will also reiterate over the command line argument topic from last week<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>The name of the input file is given as a command line argument\n<ul>\n<li>The name is always the first argument passed<\/li>\n<li>If there is no input file, the program must close itself gracefully<\/li>\n<\/ul>\n<\/li>\n<li>Read an unknown number of integers from a file. Numbers are separated either by a space or a newline. There can be infinitely many numbers.\n<ul>\n<li>Example: <span class=\"lang:default highlight:0 decode:true crayon-inline\">5 3 -6 0 25 955 -1024<\/span><\/li>\n<\/ul>\n<\/li>\n<li>The numbers will be written into output files based on the following principles:\n<ul>\n<li>0 or lower numbers are ignored<\/li>\n<li>Positive odd numbers are written to the file <span class=\"lang:default highlight:0 decode:true crayon-inline \">odd.txt<\/span><\/li>\n<li>Positive even numbers are written to the file <span class=\"lang:default highlight:0 decode:true crayon-inline\">even.txt<\/span><\/li>\n<\/ul>\n<\/li>\n<li>Follow the basic best practices for working with files\n<ul>\n<li>Check if the file opened<\/li>\n<li>Notify the user if the file doesn&#8217;t open that includes the name of the file which failed to open<\/li>\n<li>Close all files before exiting<\/li>\n<\/ul>\n<\/li>\n<li>If the input file is corrupted, the program must halt upon detecting it\n<ul>\n<li>E.g. <span class=\"lang:default highlight:0 decode:true crayon-inline \">9 -5 hey 14<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Warnings_and_hints\"><\/span>Warnings and hints!<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Be very careful with infinite loops when writing to files. You can fill up the drive space in just a few seconds. This also causes physical wear on SSDs and can kill your operating system.<\/li>\n<li>Write the output on the screen during testing so you can follow along what&#8217;s happening and to which file data is written to. This way it is easier to detect if something goes wrong and terminate the program (<span class=\"lang:default highlight:0 decode:true crayon-inline \">ctrl+c<\/span> ) .<\/li>\n<li>If the first file opened successfully, but the second one didn&#8217;t, you must close the first one before exiting!<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Extra_task_1_W15-3_Statistics\"><\/span>Extra task 1 [W15-3]: Statistics<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Add the following statistical features to the base task<\/p>\n<ul>\n<li>Find and print\n<ul>\n<li>Sum and arithmetic mean<\/li>\n<li>Smallest and greatest number<\/li>\n<\/ul>\n<\/li>\n<li>The results must cover all the values from the input file regardless of the value (this includes negative numbers)<\/li>\n<li>You&#8217;re not allowed to read the file more than once<\/li>\n<li>Program must still work with infinitely many numbers<\/li>\n<\/ul>\n<p>Extend support for command line arguments by adding support for verbose mode (&#8220;chatty&#8221;)<\/p>\n<ul>\n<li>All features from the base task must remain functioning<\/li>\n<li>Your program must detect if the argument <span class=\"lang:default highlight:0 decode:true crayon-inline \">-v<\/span>\u00a0 (verbose) was passed to it<\/li>\n<li>Program must display when a file is opened, which file it was and in which mode it was opened in.<\/li>\n<li>Program must display all numbers read from the input<\/li>\n<li>Program must display if the value was written to an output file, including which file, or discarded<\/li>\n<li>Program will display closure of each file<\/li>\n<li>If verbose is not active, the program should not print any lines in the output<\/li>\n<\/ul>\n<p>Verbose is a widely used practice to simplify both debugging programs and help with using the programs to figure out what is going wrong when using them (e.g. operator error). This is more widely used in command line based programs, but also available in many graphical programs.<\/p>\n<p>Examples of how the program should be executed:<\/p>\n<ul>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline \">.\/parity input_nums.txt<\/span><\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline \">.\/parity input_nums.txt -v<\/span><\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"Task_2_W15-2_Processing_penalty_notices\"><\/span>Task 2 [W15-2]: Processing penalty notices<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Your task is to create a program that can compose penalty notices from speed camera measurement data.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Additional_references\"><\/span><strong>Additional references<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>The task is composed based on the following public information<\/p>\n<ul>\n<li><a href=\"https:\/\/transpordiamet.ee\/en\/roads-waterways-airspace\/traffic-management\/cameras-speed-limit-enforcement\">https:\/\/transpordiamet.ee\/en\/roads-waterways-airspace\/traffic-management\/cameras-speed-limit-enforcement<\/a><\/li>\n<li><a href=\"https:\/\/www.politsei.ee\/et\/juhend\/kiiruskaamerad\/korduma-kippuvad-kusimused\">https:\/\/www.politsei.ee\/et\/juhend\/kiiruskaamerad\/korduma-kippuvad-kusimused<\/a> (Estonian only)<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Principles_of_composing_penalty_notices\"><\/span><strong>Principles of composing penalty notices<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>The measurement uncertainty is 4 km\/h (50 &#8211; 90 km\/h speed limit area)<\/li>\n<li>The processing of the penalty is started if the vehicle exceeded the speed limit by at least 3 km\/h.<\/li>\n<li>Every km\/h that is exceeded from the limit is fined by 7\u20ac.<\/li>\n<li>Maximum penalty is 420\u20ac.<\/li>\n<li>If the speed limit was exceeded by more than 50 km\/h, a general procedure is started.<\/li>\n<li>E.g. If a vehicle is driving 99 km\/h and the speed limit is set at 90 km\/h, the fine is going to be 35\u20ac.<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Input_file\"><\/span>Input file<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p><strong>Download your input file from here: <a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/andmefailid\/14_2_speeds.txt\">14_2_speeds.txt<\/a><\/strong><\/p>\n<p>Input file contains one measurement per line. The structure of the input file is: <span class=\"lang:default highlight:0 decode:true crayon-inline\">&lt;registration number&gt; &lt;measured speed&gt; &lt;speed limit&gt;<\/span><\/p>\n<ul>\n<li>Car registration number &#8211; up to 9 characters<\/li>\n<li>Measured speed &#8211; positive integer<\/li>\n<li>Speed limit &#8211; positive integer<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Output_files\"><\/span>Output files<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Your program must create two output files &#8211; one with processed fines and another for general procedure notices.<\/p>\n<p><strong>First file will contain fines.<\/strong> This file will only contain\u00a0 fine notices that the owners of the respective vehicles need to pay. Each fine notice will be written on a separate line. Every line has three data fields:<\/p>\n<ul>\n<li>Car registration number<\/li>\n<li>Exceeded speed without the measurement uncertainty (how many km\/h in excess was measured)<\/li>\n<li>Penalty (amount to be paid)<\/li>\n<\/ul>\n<p>Expected output:<\/p>\n<pre class=\"toolbar:2 lang:default highlight:0 decode:true \">444AAD 3 km\/h 21.00 EUR\r\n554OAP 16 km\/h 112.00 EUR\r\n879IIM 3 km\/h 21.00 EUR\r\n999PPP 30 km\/h 210.00 EUR\r\n888RTA 49 km\/h 343.00 EUR\r\n<\/pre>\n<p><strong>The contents of the second file are notices for general procedure.<\/strong> You can only write those entries against whom the general procedure will be started. The format for those notices is as follows:<\/p>\n<pre class=\"toolbar:2 lang:default highlight:0 decode:true\">&lt;registration number&gt;\r\nSpeed limit: &lt;speed limit&gt;\r\nMeasured speed: &lt;measured speed&gt;\r\nExceeded speed: &lt;exceeded speed&gt;\r\n<\/pre>\n<p>Expected output:<\/p>\n<pre class=\"toolbar:2 lang:default highlight:0 decode:true\">SPEED1\r\nPiirkiirus: 90 km\/h\r\nM\u00f5\u00f5detud kiirus: 231 km\/h\r\n\u00dcletus: 137 km\/h\r\n \r\n881LLK\r\nPiirkiirus: 90 km\/h\r\nM\u00f5\u00f5detud kiirus: 144 km\/h\r\n\u00dcletus: 50 km\/h\r\n \r\n648RQE\r\nPiirkiirus: 90 km\/h\r\nM\u00f5\u00f5detud kiirus: 160 km\/h\r\n\u00dcletus: 66 km\/h<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Extra_task_W15-4_Settings\"><\/span>Extra task [W15-4]: Settings<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Due to regulations and laws changing, it must be possible to easily adjust the settings that the application is using. Figure out a way to implement it so that the following requirements would be satisfied.<\/p>\n<p><strong>1. settings for processing of notices must be configurable:<\/strong><\/p>\n<ul>\n<li>Fine size for every exceeded km\/h (e.g. 3\u20ac, 5\u20ac, 10\u20ac)<\/li>\n<li>Maximum fine size (e.g. 100\u20ac, 190\u20ac, 300\u20ac)<\/li>\n<li>Speed excess to start the general procedure (E.g. 40 km\/h, 61 km\/h, 100 km\/h)<\/li>\n<\/ul>\n<p><strong>2. adjusting settings must be simple, but stay invisible during day-to-day operations<\/strong><\/p>\n<ul>\n<li>Adding the ability to adjust settings must not add extra time to use the program normally.<br \/>\n<em><strong>Thought:<\/strong> The officials working with the application on a daily basis don&#8217;t want to waste time on adjusting settings or even confirming current settings each time they use it.<\/em><\/li>\n<li>Changes must persistent<br \/>\n<em><strong>Thought:<\/strong> Values will typically be updated when the laws are changed. It is done quite seldom, so the operator doesn&#8217;t want to (re)configure it on a daily basis.<\/em><\/li>\n<li>Changing the setting must not require recompilation of the program.<br \/>\n<em><strong>Thought<\/strong>: You wouldn&#8217;t expect officials to know how to edit source code or recompile a program. Even if done by the IT department, they are just support, not developers.<\/em><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"After_this_class_you_should\"><\/span>After this class, you should<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>know how to open and close files.<\/li>\n<li>know how to check if a file opened.<\/li>\n<li>know the difference between file modes and how they behave if the file exists and if it does not.<\/li>\n<li>know in which situations files may fail to open when opened for reading or for writing.<\/li>\n<li>know different ways of addressing files.<\/li>\n<li>know why buffering files is important and what problems it may cause.<\/li>\n<li>know how to read and write files.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Additional_content\"><\/span>Additional content<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Basics: file handling in C<br \/>\n<strong><a href=\"https:\/\/www.geeksforgeeks.org\/basics-file-handling-c\/\">https:\/\/www.geeksforgeeks.org\/basics-file-handling-c\/<\/a><\/strong><\/li>\n<li>FILE data type<br \/>\n<a href=\"https:\/\/cplusplus.com\/reference\/cstdio\/FILE\/\"><strong>https:\/\/cplusplus.com\/reference\/cstdio\/FILE\/<\/strong><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Lab materials Slides: Files Example: Managing multiple files Example: File name as a command line argument Lab tasks This lab has two tasks, both of which have an extra task extending the base features. Task 1 [W15-1]: Odd or even The purpose of this task is to practice using multiple files at the same time &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1en15-files\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">PR1EN15: Files<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-6537","post","type-post","status-publish","format-standard","hentry","category-labs"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/6537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=6537"}],"version-history":[{"count":8,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/6537\/revisions"}],"predecessor-version":[{"id":10994,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/6537\/revisions\/10994"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=6537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/categories?post=6537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/tags?post=6537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}