{"id":1638,"date":"2014-06-17T13:43:10","date_gmt":"2014-06-17T13:43:10","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1638"},"modified":"2014-09-26T15:05:12","modified_gmt":"2014-09-26T15:05:12","slug":"searching-a-text-file","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/searching-a-text-file","title":{"rendered":"Searching a text file"},"content":{"rendered":"<h4>\nSearch for strings within a text file<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\r\n\t#include &lt;stdlib.h&gt;\r\n\t#include &lt;stdio.h&gt;\r\n\t#include &lt;string.h&gt;\r\n\t\t\t\t\r\n\tconst char email_receive_set_output_high_string[] = {&quot;set_output_high&quot;};\r\n\tconst char email_receive_set_output_low_string[] = {&quot;set output low&quot;};\r\n\r\n\t\t\t\t\t\r\n\t\r\n\tFILE *fp=fopen(&quot;\/home\/pi\/myproject\/somefile&quot;,&quot;r&quot;);\r\n\tchar  tmp[1024] = {0x0};\r\n\twhile(fp != NULL &amp;&amp; fgets(tmp, sizeof(tmp), fp) != NULL)\t\t\/\/fgets gets one line at a time, up to the max size - not the next block of max size.  So ensure the size is &gt;= to the max line length within the file to ensure complete lines will be checked.\r\n\t{\r\n\t\tif (strstr(tmp, email_receive_set_output_high_string))\t\t\/\/Do lines contain string \r\n\t\t{\r\n\t\t\t\/\/STRING FOUND\r\n\t\t\tprintf(&quot;Setting output high\\n&quot;);\r\n\t\t\tOUR_OUTPUT_PIN(1);\r\n\t\t\t\r\n\t\t}\r\n\t\tif (strstr(tmp, email_receive_set_output_low_string))\r\n\t\t{\r\n\t\t\t\/\/STRING FOUND\r\n\t\t\tprintf(&quot;Setting output low\\n&quot;);\r\n\t\t\tOUR_OUTPUT_PIN(0);\r\n\t\t}\r\n\t\t\r\n\t}\r\n\tif(fp != NULL)\r\n\t\tfclose(fp);\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Search for strings within a text file #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; const char email_receive_set_output_high_string[] = {&quot;set_output_high&quot;}; const char email_receive_set_output_low_string[] = {&quot;set output low&quot;}; FILE *fp=fopen(&quot;\/home\/pi\/myproject\/somefile&quot;,&quot;r&quot;); char tmp[1024] = {0x0}; while(fp != NULL &amp;&amp; fgets(tmp, sizeof(tmp), fp) != NULL) \/\/fgets gets one line at a time, up to the max size &#8211; not the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-1638","post","type-post","status-publish","format-standard","hentry","category-file-input-and-output"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1638"}],"version-history":[{"count":1,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1638\/revisions"}],"predecessor-version":[{"id":1639,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1638\/revisions\/1639"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}