Remove Text Before Marker From File

#include <fstream>			//Used for EventsToSendQueue
#include <unistd.h>			//Used for EventsToSendQueue

	string next_line;
	size_t find_result;
	bool found_marker = false;
	string EventsFilePath_Temp;
	string s_temp;
	
	EventsFilePath_Temp = EventsFilePath + ".temp";
	
	
	//----- OPEN THE FILES -----
	std::ifstream ifstream1(EventsFilePath.c_str());
	std::ofstream ofstream1(EventsFilePath_Temp.c_str(), std::ios_base::out);				//Overwrite an existing file if present
	if ( (ifstream1.is_open()) && (ofstream1.is_open()) )
	{
		while (getline(ifstream1, next_line))
		{
			//----- GET NEXT LINE -----
			if (found_marker)
			{
				ofstream1 << next_line << "\n";				//The "\n" is stripped by getline()
			}
			
			if ((find_result = next_line.find("[EVENT_END_YqzlQ2W4GOrkWr4qH]")) != string::npos)
			{
				//----- MARKER FOUND -----
				found_marker = true;
				s_temp = next_line.substr((find_result + 29));
				if (s_temp.length() > 0)
					ofstream1 << s_temp << "\n";				//The "\n" is stripped by getline()
			}
		}
		//----- CLOSE THE FILES -----
		ifstream1.close();
		ofstream1.close();
		
		//----- OVERWRITE THE OLD FILE WITH THE NEW FILE -----
		unlink(EventsFilePath.c_str());
		rename(EventsFilePath_Temp.c_str(), EventsFilePath.c_str());
		
	}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *