C++ searching a .txt file for all occurrences of a string and listing all results

I am trying to write a console utility for maintaining a court date list in .txt format so I can keep better and easier track of them. To that effect I started with a piece of code I liked, as a template, and worked my way upwards.

The problem I am encountering is with implementing a search function that goes through the whole file and lists all found occurrences of a string (namely a calendar type date stored in the format DDMMYY).

Sorry if my coding is far from being acceptable, but keep in mind that I'm just a computer passionate lawyer! :)

Without further jib-jabbing here is the code in question:

#include "stdafx.h"


using namespace std;

void name();
void ID();

int main()
{
    system("cls");
    system("color 0F");
    int ch1;
    cout << "1 pentru a introduce un termen nou" << endl;
    cout << "2 pentru a cauta o data" << endl; 
    cin >> ch1;
    if (ch1 == 1)
    {
        name();
    }
    else if (ch1 == 2)
    {
        ID();
    }
}

void name()
{
    system("cls");
    string name, salary;
    ofstream worker("termene.txt", ios::app);
    int ID;
    cout << "Termen la instanta : ";
    cin >> name;
    cout << "Data termenului format ZZLLAA : ";
    cin >> ID;
    cout << "Client : ";
    cin >> salary;

    worker << name << " " << ID << " " << salary << endl;
    worker.close();
    cin.get();
    main();
}

void ID()
{
    ifstream worker("termene.txt");
    string name, salary, ID;
    unsigned int curLine = 0;
    string search;
    cout << "Introdu data format ZZLLAA : ";
    cin >> search;
    string line, line2, line3;
    if (worker.is_open())
    {
        while ((getline(worker, line)))
        {

            if (line.find(search, 0) != string::npos)
            {
                cout << "Termen: " << endl;
                cout << line << endl;
                if (line.find(search, 1) != string::npos)
                {
                    getline(worker, line2);
                    cout << "Termen: " << endl;
                    cout << line2 << endl;
                    if (line.find(search, 2) != string::npos)
                    {
                        getline(worker, line3);
                        cout << "Termen :" << endl;
                        cout << line3 << endl;
                    }
                }
                else
                {
                    system("cls");
                    cout << "Bad command or court date." << endl;

                }
            }
            worker.close();
        }
        system("pause");
        main();
    }
}

Answers


Call main() does not look like a good idea. It can potentially create a endless loop which will consume the function call stack. Here the growing is limited by escaping standard input, but it still overloads and may overflow the call stack. Bad practice, avoid it. Create a while loop within main().

The following code will prevent call stack overflow:

int main() {
   while(true) {
      // Branch your code
   }
   return 0;
}

Then, each time a function is called within the while loop, the call stack is released when the function exits. Therefore the stack does not grow forever and you avoid the stack overflow. By the way it is better to provide an exit condition instead of creating an endless loop.


Why don't you use fgrep? of course this will only work on Unix.

fgrep COMMAND:

fgrep command is used to search one or more files for lines that match the given string or word. fgrep is faster than grep search, but less flexible: it can only find fixed text, not regular expressions.

I think you can use UnixUtils to get a few commands on Windows as well.


Need Your Help

Sqlite3 multiple arguments in bash script

bash shell sqlite

I'm new to sqlite3 but I'm starting to get the hang of it. I'm creating a simple command line based tool to remove entries from a database. The command I'm using works fine in interactive mode. It ...

How to make my collision check with Intersect between rectangles to work?

java swing graphics intersect breakout

EDIT:here is the full code: https://dl.dropboxusercontent.com/u/65678182/assignment1.rar Any possible help is highly appreciated!