{"id":6995,"date":"2022-08-15T12:54:17","date_gmt":"2022-08-15T10:54:17","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=6995"},"modified":"2026-06-19T15:34:44","modified_gmt":"2026-06-19T13:34:44","slug":"file-name-as-command-line-argument","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/file-name-as-command-line-argument\/","title":{"rendered":"File name as command line argument"},"content":{"rendered":"<p>The following example program must be executed with a command line argument representing the name of the text file we wish to read. The file must contain integers, separated by either a space or a newline. Reading will be halted when a nonnumeric value is encountered of the file has ended.<\/p>\n<p>The program uses basic error checking &#8211; it validates the number of command line arguments, checks if the file opened successfully, checks the data it reads from the file. The example code is not able to distinguish between successfully reading until the end of the file\u00a0 or encountering an error reading it (e.g. file had an alphabetical character).<\/p>\n<pre class=\"toolbar:1 lang:default decode:true \" title=\"file_args.c\">\/**\r\n * File:        file_args.c\r\n * Author:      Risto Heinsar\r\n * Created:     15.08.2022\r\n * Modified:    19.06.2026\r\n *\r\n * Description: Example on accepting the name of the input file\r\n *              as the command line argument. Argument must be provided\r\n *              when using the program.\r\n *              Input file should contain integers only.\r\n *\r\n * Usage:       .\/program_name input_file_name\r\n *\/\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define ARGS_COUNT             2\r\n\r\n#define ARGS_POS_EXEC_NAME     0\r\n#define ARGS_POS_FILE_NAME     1\r\n\r\nvoid DisplayNumsInFile(const char *fName);\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    \/\/ Check that argument was provided\r\n    if (argc != ARGS_COUNT)\r\n    {\r\n        printf(\"Usage %s &lt;filename&gt;\\n\", argv[ARGS_POS_EXEC_NAME]);\r\n        return EXIT_FAILURE;\r\n    }\r\n\r\n    DisplayNumsInFile(argv[ARGS_POS_FILE_NAME]);\r\n\r\n    return EXIT_SUCCESS;\r\n}\r\n\r\nvoid DisplayNumsInFile(const char *fName)\r\n{\r\n    \/\/ Open the file pointed by the first argument\r\n    FILE *fListOfNums = fopen(fName, \"r\");\r\n    if (fListOfNums == NULL)\r\n    {\r\n        printf(\"Error opening file \\\"%s\\\".\\n\", fName);\r\n        exit(EXIT_FAILURE);\r\n    }\r\n\r\n    int number;\r\n    int count = 1;\r\n\r\n    \/\/ Loop through the file\r\n    while (fscanf(fListOfNums, \"%d\", &amp;number) == 1)\r\n    {\r\n        printf(\"%d. %d\\n\", count, number);\r\n        count++;\r\n    }\r\n    printf(\"End of file or unexpected format reached!\\n\");\r\n\r\n    \/\/ Clean up\r\n    fclose(fListOfNums);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following example program must be executed with a command line argument representing the name of the text file we wish to read. The file must contain integers, separated by either a space or a newline. Reading will be halted when a nonnumeric value is encountered of the file has ended. The program uses basic &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/file-name-as-command-line-argument\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">File name as command line argument<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1521,"menu_order":11,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-6995","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/6995","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=6995"}],"version-history":[{"count":3,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/6995\/revisions"}],"predecessor-version":[{"id":11509,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/6995\/revisions\/11509"}],"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=6995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}