{"id":2991,"date":"2019-01-30T15:34:09","date_gmt":"2019-01-30T15:34:09","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2991"},"modified":"2019-01-30T16:19:46","modified_gmt":"2019-01-30T16:19:46","slug":"read-text-file","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/file-operations\/read-text-file","title":{"rendered":"Read Text File"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Read a text file looking for string located between two text markers<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;fstream>\n\t\n\t\n\tstring extracted_string;\n\tstd::string next_line;\n\tsize_t find_result;\n\tbool found_start = false;\n\t\n\textracted_string = \"\";\n\t\n\t\/\/We open the file and look for the ascii text between a start marker and end marker and return the first found.\n\t\n\t\/\/----- OPEN THE FILE -----\n\tstd::ifstream ifstream1(EventsFilePath.c_str());\n\tif (ifstream1.is_open())\n\t{\n\t\twhile (getline(ifstream1, next_line))\n\t\t{\n\t\t\t\/\/----- GET NEXT LINE -----\n\t\t\tif (found_start)\n\t\t\t{\n\t\t\t\textracted_string += next_line;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif ((find_result = next_line.find(\"[EVENT_START_YqzlQ2W4GOrkWr4qH]\")) != string::npos)\n\t\t\t\t{\n\t\t\t\t\t\/\/----- START MARKER FOUND -----\n\t\t\t\t\tcout  &lt;&lt; \"Starts at \" &lt;&lt; find_result &lt;&lt; endl;\n\t\t\t\t\tfound_start = true;\n\t\t\t\t\textracted_string = next_line.substr((find_result + 31), string::npos); \n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif (found_start)\n\t\t\t{\n\t\t\t\tif ((find_result = extracted_string.find(\"[EVENT_END_YqzlQ2W4GOrkWr4qH]\")) != string::npos)\n\t\t\t\t{\n\t\t\t\t\t\/\/----- END MARKER FOUND -----\n\t\t\t\t\tcout  &lt;&lt; \"End at \" &lt;&lt; find_result &lt;&lt; endl;\n\t\t\t\t\t\/\/extracted_string = extracted_string.substr(0, extracted_string.find(\"[EVENT_END_YqzlQ2W4GOrkWr4qH]\"));\n\t\t\t\t\textracted_string = extracted_string.substr(0, find_result);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tifstream1.close();\n\t}\n\t\n\tcout  &lt;&lt; \"Extracted string: \" &lt;&lt; extracted_string &lt;&lt; endl;\n    return(extracted_string);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Read a text file looking for string located between two text markers<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-2991","post","type-post","status-publish","format-standard","hentry","category-file-operations"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2991","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=2991"}],"version-history":[{"count":2,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2991\/revisions"}],"predecessor-version":[{"id":2995,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2991\/revisions\/2995"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}