{"id":1368,"date":"2023-03-03T10:47:36","date_gmt":"2023-03-03T10:47:36","guid":{"rendered":"https:\/\/ibex.tech\/c\/?p=1368"},"modified":"2024-04-24T16:57:48","modified_gmt":"2024-04-24T15:57:48","slug":"file-functions-reading","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/stream\/file-functions-reading","title":{"rendered":"File functions-Reading"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">fgetc<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tint NextChar;\n\tif ((NextChar = fgetc(File1)) == EOF)\t\t\t\/\/Returns character read as an unsigned char or EOF on end of file or error\n\t{\n\t\t\/\/END OF FILE REACHED\n\t}\n\telse\n\t{\n\t\t*pDest++ = (char)NextChar;\n\t}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">fgets<\/h4>\n\n\n\n<p>Return next line from a file, or the portion of it up to the buffer size limit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tchar LineBuffer&#91;30];\n\twhile (fgets(LineBuffer, sizeof(LineBuffer), File1))\n\t{<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">A better way of doing it<\/h5>\n\n\n\n<p>fgets() doesn&#8217;t add a null character at the end of the read data and on some systems can fail to handle mac formatted text files correctly (0x0d only line endings). The following code ensures there is a terminating null, handles all line-ending character possibilities and also handles lines that are too long by dumping the remainder of the line (you can change that behaviour if you want):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Returns pointer to start of the line, or 0x00 if EOF and no bytes read\nchar *fgets_our_version (char *LineBuffer, int Size, FILE *File1)\n{\n\tint Index;\n\tstatic int GetcNext = -1;\t\t\t\/\/Needs to be retained for the next call\n\tint GetcPrevious;\n\n\tIndex = 0;\n\twhile (1)\n\t{\n\t\tGetcPrevious = GetcNext;\n\t\tGetcNext = fgetc(File1);\n\n\t\tif (GetcNext == EOF)\n\t\t{\n\t\t\t\/\/ESP_LOGI(TAG, \"Got EOF\");\n\t\t\t\/\/Error or end of file\n\t\t\tLineBuffer&#91;Index] = 0x00;\n\t\t\tif (Index == 0)\n\t\t\t\treturn (0x00);\n\t\t\telse\n\t\t\t\treturn (LineBuffer);\n\t\t}\n\t\telse if (\n\t\t\t(GetcPrevious == '\\r') &amp;&amp;\n\t\t\t(GetcNext == '\\n')\n\t\t)\n\t\t{\n\t\t\t\/\/Ignore &#91;0x0a] when line ending is &#91;0x0d]&#91;0x0a]\n\t\t\t\/\/ESP_LOGI(TAG, \"Got CRNL\");\n\t\t}\n\t\telse if (\n\t\t\t(GetcNext == '\\r') ||\t\t\/\/We handle line endings with &#91;0x0a] (Linux), &#91;0x0d]&#91;0x0a] (Windows), &#91;0x0d] (Mac)\n\t\t\t(GetcNext == '\\n')\n\t\t)\n\t\t{\n\t\t\t\/\/End of line\n\t\t\t\/\/ESP_LOGI(TAG, \"Got NL\");\n\t\t\tLineBuffer&#91;Index] = 0x00;\n\t\t\treturn (LineBuffer);\n\t\t}\n\t\telse\n\t\t{\n\t\t\t\/\/Get next character\n\t\t\tif (Index &lt; (Size - 1))\t\t\/\/Leave space for the terminating null\n\t\t\t\tLineBuffer&#91;Index++] = (char)GetcNext;\n\t\t}\n\t}\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">fread<\/h4>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>fgetc fgets Return next line from a file, or the portion of it up to the buffer size limit: A better way of doing it fgets() doesn&#8217;t add a null character at the end of the read data and on some systems can fail to handle mac formatted text files correctly (0x0d only line endings). [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-1368","post","type-post","status-publish","format-standard","hentry","category-stream"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=1368"}],"version-history":[{"count":10,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1368\/revisions"}],"predecessor-version":[{"id":1560,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1368\/revisions\/1560"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=1368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=1368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=1368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}