Read a text file looking for string located between two text markers

#include <fstream>
	
	
	string extracted_string;
	std::string next_line;
	size_t find_result;
	bool found_start = false;
	
	extracted_string = "";
	
	//We open the file and look for the ascii text between a start marker and end marker and return the first found.
	
	//----- OPEN THE FILE -----
	std::ifstream ifstream1(EventsFilePath.c_str());
	if (ifstream1.is_open())
	{
		while (getline(ifstream1, next_line))
		{
			//----- GET NEXT LINE -----
			if (found_start)
			{
				extracted_string += next_line;
			}
			else
			{
				if ((find_result = next_line.find("[EVENT_START_YqzlQ2W4GOrkWr4qH]")) != string::npos)
				{
					//----- START MARKER FOUND -----
					cout  << "Starts at " << find_result << endl;
					found_start = true;
					extracted_string = next_line.substr((find_result + 31), string::npos); 
				}
			}
			
			if (found_start)
			{
				if ((find_result = extracted_string.find("[EVENT_END_YqzlQ2W4GOrkWr4qH]")) != string::npos)
				{
					//----- END MARKER FOUND -----
					cout  << "End at " << find_result << endl;
					//extracted_string = extracted_string.substr(0, extracted_string.find("[EVENT_END_YqzlQ2W4GOrkWr4qH]"));
					extracted_string = extracted_string.substr(0, find_result);
					break;
				}
			}
		}
		ifstream1.close();
	}
	
	cout  << "Extracted string: " << extracted_string << endl;
    return(extracted_string);
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 *