{"id":1952,"date":"2016-06-11T00:01:52","date_gmt":"2016-06-11T07:01:52","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1952"},"modified":"2016-06-04T11:22:02","modified_gmt":"2016-06-04T18:22:02","slug":"all-files-have-a-number","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1952","title":{"rendered":"All Files Have a Number"},"content":{"rendered":"<p>The common way for humans to describe a file is to use its name. You refer to <code>hello.txt<\/code> as a file. The <em>fopen()<\/em> function C uses this nomenclature, which is handy and convenient. While a program runs, however, a file number is assigned to an open file.<br \/>\n<!--more--><br \/>\nThe file number is an integer. It&#8217;s assigned sequentially, starting with 0. The first three numbers are reserved to the standard I\/O devices: <code>stdin<\/code>, <code>stdout<\/code>, and <code>stderr<\/code>. After that, any files you open in a program use a number as a reference.<\/p>\n<p>The number itself is trivial. I&#8217;m sure it&#8217;s used internally, but within your code you&#8217;ll use a <code>FILE<\/code> variable to reference the file for reading, writing, and eventually closing.<\/p>\n<blockquote><p>In Unix, you can use the <code>ulimit<\/code> command in bash determine the maximum allowed number of open files. On my Mac, it&#8217;s 256. In Windows, the limit appears to be set by the C runtime library to 512, but that information is inconsistent. Back under DOS, the <code>FILES<\/code> command in <code>CONFIG.SYS<\/code> set the maximum number of open files.<\/p><\/blockquote>\n<p>To glean the file number for any open file, you use the <em>fileno()<\/em> function. It&#8217;s defined in the <code>stdio.h<\/code> header and its man page format looks like this:<\/p>\n<pre><code>int fileno(FILE *stream)<\/code><\/pre>\n<p>The <em>stream<\/em> represents an open file, or the three standard I\/O devices. The value returned is an integer representing the file number.<\/p>\n<p>Here is a sample program that displays the file number values for the standard I\/O devices:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"File number for 'stdin' is %d\\n\",fileno(stdin));\r\n    printf(\"File number for 'stdout' is %d\\n\",fileno(stdout));\r\n    printf(\"File number for 'stderr' is %d\\n\",fileno(stderr));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is the program&#8217;s output:<\/p>\n<pre><code>File number for 'stdin' is 0\r\nFile number for 'stdout' is 1\r\nFile number for 'stderr' is 2<\/code><\/pre>\n<p>These file numbers are consistent for the three standard I\/O devices on all computers at all times. When you open another file, it&#8217;s assigned the next highest value, as this code demonstrates:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    FILE *f;\r\n\r\n    f = fopen(\"hello.txt\",\"w\");\r\n    if( f == NULL)\r\n    {\r\n        puts(\"Error creating file.\");\r\n        return(1);\r\n    }\r\n    printf(\"File number for 'hello.txt' is %d\\n\",fileno(f));\r\n    fclose(f);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is sample output:<\/p>\n<pre><code>File number for 'hello.txt' is 3<\/code><\/pre>\n<p>So what&#8217;s the point?<\/p>\n<p>Really, there isn&#8217;t any point to a file descriptor outside of the Unix environment. In Windows, the numbers are assigned, but not needed anywhere else.<\/p>\n<p>In Unix, you can use the descriptor with the low-level <em>read()<\/em> and <em>write()<\/em> functions. Even so, experienced programmers recommend that you instead use the <code>FILE<\/code> variable and the standard <em>fopen()<\/em>, <em>fread()<\/em>, <em>fwrite()<\/em>, and similar functions. That leaves the <em>fileno()<\/em> function a side note. Still, I think it&#8217;s interesting that the three standard I\/O devices are always open and always assigned the same file number values.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>fileno()<\/em> function is interesting but rather trivial. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1952\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1952","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1952"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1952\/revisions"}],"predecessor-version":[{"id":1974,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1952\/revisions\/1974"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}