Find specific lines in text document

user6676275

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 = 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<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;
}
naffarn

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find specific text in many lines with Perl

From Dev

Separating a text document by specific lines of text using python

From Dev

how to read specific lines of a text document and write them to another text | C

From Dev

Split text lines in scanned document

From Dev

Find and replace text in a document

From Dev

Mark specific lines of paragraph text

From Dev

Insert text in specific lines of a file

From Dev

find certain text in lines, then hide those lines

From Dev

Regular expression to find the text lines that do not contain a specific string using grep(linux terminal-opensuse)

From Dev

Is there a command-line utility app which can find a specific block of lines in a text file, and replace it?

From Dev

Is there a command-line utility app which can find a specific block of lines in a text file, and replace it?

From Dev

Regular expression to find the text lines that do not contain a specific string using grep(linux terminal-opensuse)

From Dev

Find empy lines in text file

From Dev

Find Sting in line of text document

From Dev

Find Sting in line of text document

From Dev

Removing lines in text document that end the same

From Dev

Remove all duplicate lines from a text document

From Dev

Find lines that DO NOT contain a specific character

From Dev

find all files and lines that contains a specific string

From Dev

using grep to find and write specific lines with conditions

From Dev

Find my lines in repository by specific pattern

From Dev

Read lines until find specific characters

From Dev

Rename specific lines in a text file using python

From Dev

Please help to remove specific lines from text

From Dev

Python - combine text files (specific lines)

From Dev

Getting specific lines in a text file and pass it to an array

From Dev

How to delete specific lines in text file?

From Dev

Sort lines in text file with specific separator in Linux

From Dev

Sort lines in text file with specific separator in Linux

Related Related

  1. 1

    Find specific text in many lines with Perl

  2. 2

    Separating a text document by specific lines of text using python

  3. 3

    how to read specific lines of a text document and write them to another text | C

  4. 4

    Split text lines in scanned document

  5. 5

    Find and replace text in a document

  6. 6

    Mark specific lines of paragraph text

  7. 7

    Insert text in specific lines of a file

  8. 8

    find certain text in lines, then hide those lines

  9. 9

    Regular expression to find the text lines that do not contain a specific string using grep(linux terminal-opensuse)

  10. 10

    Is there a command-line utility app which can find a specific block of lines in a text file, and replace it?

  11. 11

    Is there a command-line utility app which can find a specific block of lines in a text file, and replace it?

  12. 12

    Regular expression to find the text lines that do not contain a specific string using grep(linux terminal-opensuse)

  13. 13

    Find empy lines in text file

  14. 14

    Find Sting in line of text document

  15. 15

    Find Sting in line of text document

  16. 16

    Removing lines in text document that end the same

  17. 17

    Remove all duplicate lines from a text document

  18. 18

    Find lines that DO NOT contain a specific character

  19. 19

    find all files and lines that contains a specific string

  20. 20

    using grep to find and write specific lines with conditions

  21. 21

    Find my lines in repository by specific pattern

  22. 22

    Read lines until find specific characters

  23. 23

    Rename specific lines in a text file using python

  24. 24

    Please help to remove specific lines from text

  25. 25

    Python - combine text files (specific lines)

  26. 26

    Getting specific lines in a text file and pass it to an array

  27. 27

    How to delete specific lines in text file?

  28. 28

    Sort lines in text file with specific separator in Linux

  29. 29

    Sort lines in text file with specific separator in Linux

HotTag

Archive