{"id":1454,"date":"2023-08-25T14:37:43","date_gmt":"2023-08-25T13:37:43","guid":{"rendered":"https:\/\/ibex.tech\/c\/?p=1454"},"modified":"2024-04-25T18:36:48","modified_gmt":"2024-04-25T17:36:48","slug":"working-with-files","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/stream\/working-with-files","title":{"rendered":"Working with Files"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Does a file exist<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const char *MyFilePath = \"\/usb\/myfile.txt\";\n\n\t\/\/----- DOES FILE EXIST -----\n\tstruct stat sb;\n\tint Result = stat(MyFilePath, &amp;sb);\n\tif ((Result != 0) || (sb.st_mode &amp; S_IFDIR))\n\t{\n\t\t\/\/FILE DOES NOT EXIST\n\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">fopen()<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const char *MyFilePath = \"\/usb\/myfile.txt\";\nFILE *File1;\n\n\tFile1 = fopen(MyFilePath , \"r\");\n\tif (File1 == NULL)\n\t{\n\t\t\/\/Failed to open file\n\n\t}\n\n\/\/access_mode\n\/\/\t\"r\"  Open a file for reading. The file must exist.\n\/\/\t\"r+\" Open a file for reading and writing. The file must exist.\n\/\/\t\"w\"  Create an empty file for writing. If a file with the same name already exists its content is erased.\n\/\/\t\"w+\" Create an empty file for writing and reading. If a file with the same name already exists its content is erased before it is opened.\n\/\/\t\"a\"  Append to a file. Write operations append data at the end of the file. The file is created if it doesn\u2019t exist.\n\/\/\t\"a+\" Open a file for reading and appending. All writing operations are done at the end of the file protecting the previous content from being overwritten. You can\n\/\/\t\t reposition (fseek) the pointer to anywhere in the file for reading, but writing operations will move back to the end of file. The file is created if it doesn\u2019t exist.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Write text file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tconst char *DOWNLOAD_LOG_FILE_PATH = \"\/usb\/esp\/test.txt\";\n\n\t\/\/----- CREATE FILE -----\n\tESP_LOGI(TAG, \"Creating file\");\n\tFILE *File1 = fopen(DOWNLOAD_LOG_FILE_PATH, \"w\");\t\/\/\"w\"  Create an empty file for writing. If a file with the same name already exists its content is erased.\n\tif (File1 == NULL)\n\t{\n\t\tprintf(\"Failed to open file for writing\\n\");\n\t\treturn;\n\t}\n\tfprintf(File1, \"Hello World!\\n\");\n\n\tint MyValue = 123;\n\tfprintf(File1, \"MyValue:%d %s\\n\", MyValue, \"some text\");\n\n\tfclose(File1);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read text file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\tconst char *DOWNLOAD_LOG_FILE_PATH file_path = \"\/usb\/esp\/test.txt\";\n\n\t\/\/----- READ FILE -----\n\tFILE *File1;\n\tESP_LOGI(TAG, \"Reading file\");\n\tFile1 = fopen(DOWNLOAD_LOG_FILE_PATH, \"r\");\n\tif (File1 == NULL)\n\t{\n\t\tprintf(\"Failed to open file for reading\\n\");\n\t\treturn;\n\t}\n\n\tchar LineBuffer&#91;64];\n\tfgets(LineBuffer, sizeof(LineBuffer), File1);\n\t\/\/fgets_our_version (&amp;LineBuffer&#91;0], sizeof(LineBuffer), File1)\t\t\/\/&lt;Or use our better version of fgets()\n\tfclose(File1);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get file length<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/*************************************\n\/\/*************************************\n\/\/********** GET FILE LENGTH **********\n\/\/*************************************\n\/\/*************************************\nlong int GetFileLength (char *FileName)\n{\n\n\t\/\/Open the file\n\tFILE *File1 = fopen(FileName, \"r\"); \n\tif (File1 == NULL)\n\t\treturn 0;\n\t\n\t\/\/Seek to the end\n\tfseek(File1, 0L, SEEK_END); \n\tlong int FileLength = ftell(File1); \n\n\t\/\/Close the file\n\tfclose(File1); \n\n\treturn(FileLength); \n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tremove(FilePath);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">printf the contents of a text file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/***************************************\n\/\/***************************************\n\/\/********** DEBUG OUTPUT FILE **********\n\/\/***************************************\n\/\/***************************************\nvoid DebugOutputFile (char* FilePath)\n{\n\tFILE *File1;\n\tchar LineBuffer&#91;512];\n\n\tprintf(\"Output of file %s:\\n\", FilePath);\n\n\tFile1 = fopen(FilePath, \"r\");\n\tif (File1 == NULL)\n\t{\n\t\tprintf(\"Failed to open file for reading\\n\");\n\t\treturn;\n\t}\n\n\twhile (fgets_our_version (&amp;LineBuffer&#91;0], sizeof(LineBuffer), File1) != 0x00)\n\t\tprintf(\"%s\\n\", LineBuffer);\n\n\tfclose(File1);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Does a file exist fopen() Write text file Read text file Get file length Delete file printf the contents of a text file<\/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-1454","post","type-post","status-publish","format-standard","hentry","category-stream"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1454","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=1454"}],"version-history":[{"count":11,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1454\/revisions"}],"predecessor-version":[{"id":1561,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1454\/revisions\/1561"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=1454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=1454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=1454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}