C++ line index in text document
<p>as beginner need your help</p>

<p>So I'm trying to make program which can write phrases into the text document, new line below previous, just in original order. It checks If phrase exist in document and shows me the row on output, if not, add it as new.</p>

<p>Now I want two thing here, and can't figure out, question is how to get index for lines, and how to manipulate with it to get lines, for example my text document content:</p>

<pre><code>word1
word2
word3
word4
word5
</code></pre>

<p>Now, if text inserted by user for example "word6", which is not exist in document, just must be added below. But if inserted text for example "word1" which is exist in document, in this case I want see on output "word2", which is below the found "word1", but if inserted text is "word2", I want see on output "word1" etc. if "word3" on output show me "word4", if "word4" show me "word3" etc, depending which index has been defined in current exist word, divisible -1 or not divisible +1 like this:</p>

<pre><code>word1 (indivisible) show word2
word2 (divisible) show word1
word3 (indivisible) show word4
word4 (divisible) show word3
</code></pre>

<hr>

<pre><code>using namespace std;

std::ofstream outfile("doc.txt", std::ios_base::app);

int main()
{

    int length = 100;

    std::ifstream infile("doc.txt", std::ifstream::in);
    infile.seekg(0, infile.end);
    size_t len = infile.tellg();
    infile.seekg(0, infile.beg);
    char *buf = new char[len];
    infile.read(buf, length);
    infile.close();
    std::string writtenStr(reinterpret_cast&lt;const char *&gt;(buf), len);

    std::string t;

    for (int i = 0; i &lt; 10; i++)
    {
        std::getline(std::cin, t);

        if (writtenStr.find(t) != std::string::npos)
        {
            cout &lt;&lt; "Line [" &lt;&lt; t &lt;&lt; "] exist." &lt;&lt; endl;
        }
        else
        {
            cout &lt;&lt; "Line [" &lt;&lt; t &lt;&lt; "] saved." &lt;&lt; endl;
            writtenStr += t;
            outfile &lt;&lt; t &lt;&lt; std::endl;
        }
    }
    _getch();
    return 0;
}
</code></pre>