Search code examples
c++extractfstreamifstreamofstream

C++ to extract data between two strings


I am looking for a c++ code which can extract some specific content from a file example.txt between two strings and ignore the rest content. for example a file example.txt have below lines

xyz
abc
['Content','en']],
<html>hi this is a line <br></html>
',true], 
suzi 20

I want to extract code between ['Content','en']], and ',true], which means

<html>hi this is a line <br></html>

Please note that i am not expert in programming and using dev++ compiler


Solution

  • The simplest idea is to read the file into a string, and then extract the content:

    #include <string>
    #include <sstream>
    
    std::string extract(std::string const& tag_begin, std::string const& tag_end, std::istream& input)
    {
        // file stream -> in memory string
        std::string filedata((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
    
        // find content start and stop
        auto content_start = filedata.find(tag_begin);
        if (content_start == std::string::npos) {
            return ""; // error handling
        }
        content_start += tag_begin.size();
        auto content_end   = filedata.find(tag_end, content_start);
        auto content_size  = content_end - content_start;
    
        // extract (copy) content to other string
        auto content = filedata.substr(content_start, content_size);
        return content;
    }
    

    live demo

    Then, you'll need to adapt this generic solution to your needs.