{"id":2996,"date":"2019-01-30T16:42:46","date_gmt":"2019-01-30T16:42:46","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2996"},"modified":"2019-01-30T16:43:52","modified_gmt":"2019-01-30T16:43:52","slug":"modify-text-file","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/file-operations\/modify-text-file","title":{"rendered":"Modify Text File"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Remove Text Before Marker From File<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;fstream>\t\t\t\/\/Used for EventsToSendQueue\n#include &lt;unistd.h>\t\t\t\/\/Used for EventsToSendQueue\n\n\tstring next_line;\n\tsize_t find_result;\n\tbool found_marker = false;\n\tstring EventsFilePath_Temp;\n\tstring s_temp;\n\t\n\tEventsFilePath_Temp = EventsFilePath + \".temp\";\n\t\n\t\n\t\/\/----- OPEN THE FILES -----\n\tstd::ifstream ifstream1(EventsFilePath.c_str());\n\tstd::ofstream ofstream1(EventsFilePath_Temp.c_str(), std::ios_base::out);\t\t\t\t\/\/Overwrite an existing file if present\n\tif ( (ifstream1.is_open()) &amp;&amp; (ofstream1.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_marker)\n\t\t\t{\n\t\t\t\tofstream1 &lt;&lt; next_line &lt;&lt; \"\\n\";\t\t\t\t\/\/The \"\\n\" is stripped by getline()\n\t\t\t}\n\t\t\t\n\t\t\tif ((find_result = next_line.find(\"[EVENT_END_YqzlQ2W4GOrkWr4qH]\")) != string::npos)\n\t\t\t{\n\t\t\t\t\/\/----- MARKER FOUND -----\n\t\t\t\tfound_marker = true;\n\t\t\t\ts_temp = next_line.substr((find_result + 29));\n\t\t\t\tif (s_temp.length() > 0)\n\t\t\t\t\tofstream1 &lt;&lt; s_temp &lt;&lt; \"\\n\";\t\t\t\t\/\/The \"\\n\" is stripped by getline()\n\t\t\t}\n\t\t}\n\t\t\/\/----- CLOSE THE FILES -----\n\t\tifstream1.close();\n\t\tofstream1.close();\n\t\t\n\t\t\/\/----- OVERWRITE THE OLD FILE WITH THE NEW FILE -----\n\t\tunlink(EventsFilePath.c_str());\n\t\trename(EventsFilePath_Temp.c_str(), EventsFilePath.c_str());\n\t\t\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remove Text Before Marker From File<\/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-2996","post","type-post","status-publish","format-standard","hentry","category-file-operations"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2996","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=2996"}],"version-history":[{"count":2,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2996\/revisions"}],"predecessor-version":[{"id":2998,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2996\/revisions\/2998"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}