John K John K - 4 months ago 25
C++ Question

Find specific lines in text document

I want to create small program to understand things I need better.

This code can write words to text document, new line under previous in sequential order and keep lines there after starting program again.

Now before adding a new word or phrase to the file, I want to find if the word already exists in my document, if exist, don't add it, but get exist equal one on output, read it from file, and main thing here is somehow also find line under or above current exist line. For example: if exist line index is 3, I want to see +1 line 4 or -1 line 2. If new word doesn't exist in text document just add it.

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

using namespace std;

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

int main()
{

std::string t;

for (int i = 0; i < 10; i++)
{
cout << "Add new phrase: " << endl;

std::getline(std::cin, t);

cout << t << endl;

outfile << t << std::endl;

}

_getch();
return 0;
}


EDIT:

using namespace std;

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

int main()
{

int length = 0;

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<const char *>(buf), len);

std::string t;

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

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

Answer

I'd read the file into a string when the program starts. Then, check the string for the phrase each time i want to add a new word. If the string doesn't contain the phrase, add it to the string and the file, and a delimiter of your choice if desired. For example:

int main()
{
    // Read existing file into a string
    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<const char *>(buf), len);

    // Open file for output
    std::ofstream outfile("doc.txt", std::ios_base::app); 
    std::string t;

    for (int i = 0; i < 10; i++)
    {
        // Get new phrase
        std::getline(std::cin, t); 
        // Check if phrase is already in file;
        if (writtenStr.find(t) == std::string::npos) 
        {
            cout << "Could not add new phrase: " << endl;
            cout << t << endl;
            cout << "Phrase already exists in file." << endl;
        } 
        else
        {
            cout << "Add new phrase: " << endl;
            cout << t << endl;
            writtenStr += t;
            outfile << t << std::endl;
        }
    }
    _getch();
    return 0;
}