Search code examples
c++searchtextifstream

How to extract a multiline text segment between two delimiters under a certain heading from a text file using C++


I have made a program that, based on user input, generates the following .txt word list:

<>fauve:

"--------------------------------------------------------------------------------------------"

Definition of fauve:

1 : of or relating to painters practicing fauvism

2 : vivid in color

Example 1 of fauve:

"Fauve colors brought sizzle back to tableware, but could you really eat off a Rorschach of orange, black and pink?" - Julie V. Iovine, The New York Times Magazine, 14 Mar. 1993

Example 2 of fauve:

"Three were landscapes.. The other was a later painting of Adele, pale and strained, standing in a big hat with her arms loose amid fauve colours of red, mauve and green." - The Economist, 19 Feb. 2011

Explanation of fauve:

When French art critic Louis Vauxcelles spotted a statue reminiscent of 15th-century Italian art in the midst of works by an avant-garde group of painters-principal among them Henri Matisse-at an exhibit in Paris in 1905, he verbalized his shock with the words "Donatello au milieu des fauves!" ("Donatello among the wild animals!"). His reaction was to the painters' unconventional use of intensely vivid color and free treatment of form, and apparently his words weren't far off the mark in describing their art: Matisse and company's art movement became known as "Fauvism" and the artists flourishing in it, the "Fauves." In 1967, the intense impact of their colors was still vibrant, inspiring one writer for Vogue to use fauve as an adjective to describe the colors of a "striking" flowered coat-and that use can still be found today vivifying colors. ————————————————————————————————————————————————————————————————————————————————————————————

(PS: the quotation marks around the line of hyphens were added so it will display as in the text file. When pasting it in directly, it just made the headline bold. It is really just the word, then carriage return, then the line of hyphens, then two carriage returns before “Definition.”)

The program appends a new entry every time it is run except when the proposed entry already exists.

What I would like to do is to create another program that captures all the text between the line of hyphens and the line of underscores and displays it based on the user input “fauve.” Below is a rough sketch of said program, in which only the code required for assigning the delimiters and printing out the text segment is missing.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string TextFile;
    cout << "Enter the wordlist to search:" << "\n";
    getline(cin, TextFile);

    string Search;
    int Offset = 0;
    string Line;
    ifstream iEnglishVocabulary;
    iEnglishVocabulary.open(TextFile + ".txt");

    string Word;
    int Dummy = 0;

    //Input

    cout << "Enter the word:" << endl;
    getline(cin, Word);

    Search = "<>" + Word + ":";
    if (iEnglishVocabulary.is_open())
    {
        while (!iEnglishVocabulary.eof())
        {
            getline(iEnglishVocabulary, Line);
            Offset = (Line.find(Search, 0));
            if (Offset != string::npos)
            {
                cout << THE ENTRY THAT FOLLOWS THE WORD ENTERED;                
            }
        }
    }        
    cin >> Dummy;  

    iEnglishVocabulary.close();

    return 0;
}

Solution

  • After taking a closer look at reading text files in C++, I settled on this passable but most likely far from ideal solution:

    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string TextFile;
        cout << "Enter the wordlist to search:" << "\n";
        getline(cin, TextFile);
    
        string Search;
        int Offset = 0;
        int Offset2 = 0;
        string Line;
        ifstream iEnglishVocabulary;
        iEnglishVocabulary.open(TextFile + ".txt");
    
        string Word;
        string EntryEnd = "________________________________________";
        int Dummy = 0;
        bool PassingEntry = 0;
        bool WordFound = 0;
    
        //Input
    
        cout << "Enter the word:" << endl;
        getline(cin, Word);
    
        Search = "<>" + Word + ":";
        if (iEnglishVocabulary.is_open())
        {
            while (!iEnglishVocabulary.eof())
            {
                getline(iEnglishVocabulary, Line);
                Offset = (Line.find(Search, 0));
                Offset2 = (Line.find(EntryEnd, 0));
                if (Offset != string::npos)
                {
                    PassingEntry = 1;
                    WordFound = 1;
                    cout << endl << endl;
                }
                if (PassingEntry == 1)
                {
                    if (Offset2 != string::npos)
                    {
                        PassingEntry = 0;
                        cout << Line << endl;
                    }
                    else
                    {
                        cout << Line << endl;
                    }
                }
            }
            if (WordFound == 0)
            {
                cout << "The word is not in the list" << endl;
            }
        }
    
        //Output
    
        cin >> Dummy;
    
        iEnglishVocabulary.close();
    
        return 0;
    }
    

    This solution does include the heading and bottom line also, as I found that worked better. If anyone has a better solution I’m sure posterity would be grateful.