{"id":2470,"date":"2016-02-05T23:36:10","date_gmt":"2016-02-05T21:36:10","guid":{"rendered":"http:\/\/blue.pri.ee\/ttu\/?page_id=2470"},"modified":"2024-08-12T16:04:27","modified_gmt":"2024-08-12T14:04:27","slug":"grades-c","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/grades-c\/","title":{"rendered":"Managing multiple files"},"content":{"rendered":"<p>This sample program reads data from a file line-by-line and if whenever it finds students who got a 5, it will write it to a second file.<\/p>\n<p>The format for the input file: &lt;name&gt; &lt;grade&gt;<\/p>\n<pre class=\"toolbar:1 lang:c highlight:0 decode:true\" title=\"Data file: grades.txt\">Marju 4\r\nOlga 5\r\nToomas 3\r\nHelen 2\r\nPeeter 5\r\nAlbert 4\r\nMadis 5<\/pre>\n<p>Notice the following:<\/p>\n<ul>\n<li>The second file is not opened before we make sure that the first one opened successfully<\/li>\n<li>If opening the output file fails, first the input file is closed and only then we exit the program.<\/li>\n<li>Notice that this program is also susceptible to buffer overflow attacks (not the scope for this sample).<\/li>\n<\/ul>\n<pre class=\"lang:c decode:true \" title=\"grades.c\">\/**\r\n * File:        grades.c\r\n * Author:      Risto Heinsar\r\n * Created:     12.02.2015\r\n * Modified:    12.08.2024\r\n *\r\n * Description: Sample code using multiple file pointers at the same time.\r\n *              It finds the students who got 5 as the grade for the subject\r\n *              and outputs it to another file.\r\n *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n\/* Names of files *\/\r\n#define F_INPUT \"grades.txt\"\r\n#define F_OUTPUT \"fivers.txt\"\r\n\r\n\/* Exit failure codes *\/\r\n#define EXIT_INPUT_FAIL -1\r\n#define EXIT_OUTPUT_FAIL -2\r\n\r\n\/* Name buffer length *\/\r\n#define NAME_LEN 128\r\n\r\nint main(void)\r\n{\r\n    \/* Declare file pointer and attempt to open a file *\/\r\n    FILE *fi = fopen(F_INPUT, \"r\");\r\n    \r\n    \/* Check if it was opened successfully *\/\r\n    if (fi == NULL)\r\n    {\r\n        printf(\"Error opening input file \\\"%s\\\"\\n\", F_INPUT);\r\n        exit(EXIT_INPUT_FAIL);\r\n    }\r\n    \r\n    \/* Open and check for output file *\/\r\n    FILE *fo = fopen(F_OUTPUT, \"w\");\r\n    if (fo == NULL)\r\n    {\r\n        printf(\"Error opening output file\\\"%s\\\"\\n\", F_OUTPUT);\r\n        \r\n        \/* Since fi was opened, we need to close it before exit *\/\r\n        fclose(fi);\r\n        exit(EXIT_OUTPUT_FAIL);\r\n    }\r\n    \r\n    int grade;\r\n    char name[NAME_LEN];\r\n    \r\n    \/* Read the names and grades *\/\r\n    while (fscanf(fi, \"%s %d\", name, &amp;grade) == 2)\r\n    {\r\n        if (grade == 5)\r\n        {\r\n            fprintf(fo, \"%s %d\\n\", name, grade);\r\n        }\r\n    }\r\n\r\n    \/* Close the files before exiting *\/\r\n    fclose(fi);\r\n    fclose(fo);\r\n    \r\n    return EXIT_SUCCESS;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This sample program reads data from a file line-by-line and if whenever it finds students who got a 5, it will write it to a second file. The format for the input file: &lt;name&gt; &lt;grade&gt; Marju 4 Olga 5 Toomas 3 Helen 2 Peeter 5 Albert 4 Madis 5 Notice the following: The second file &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/grades-c\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Managing multiple files<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1521,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width.php","meta":{"footnotes":""},"class_list":["post-2470","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"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=2470"}],"version-history":[{"count":2,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2470\/revisions"}],"predecessor-version":[{"id":9458,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2470\/revisions\/9458"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/1521"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=2470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}