Search code examples
c++fstream

How to delete a specific line/string from a text file (fstream/c++)


This function is meant to remove a specific line from a database text file.

void removeEntry() {
    fstream outputFile("outputFileName", std::fstream::in | std::fstream::out | std::fstream::app);
    fstream database("database", std::fstream::in | std::fstream::out | std::fstream::app);
    string line;
    string find;
    string var;
    cout << "Search for the entry you want to remove (type, category, amount, date [02-21-2021]): ";
    cin.ignore(256, '\n');
    getline(cin, find);

    while(getline(database, line)){
        if(line.find(find) != string::npos) {
            var = line;
            cout << line << endl;
            string choice{};
            cout << "Is this the line you want to delete[y/n]: ";
            cin >> choice;  

            if (choice =="y")
            {
                while(getline(database, line)){
                    if(line != var){
                        outputFile << line << endl;
                    }
                }
                database.close();
                outputFile.close();
                remove("database");
                rename("outputFileName","database");
            } else {
                continue;
            }
        } 
    }
}

The database text file is formatted as so:

Expense, Transportation, $56, 02-08-2021
Income, Salary, $7800, 02-09-2021
Expense, Food, $157, 02-10-2021

If I run the program and try and remove the first line for example, it works, but if I try and remove the second line only, it will remove both the first and the second. I understand the solution may be simple but it is going over my head. I can provide any further details needed, thank you!


Solution

  • Let's think about the structure of your program:

    while(getline(database, line)){
        if(line.find(find) != string::npos) {
            var = line;
            cout << line << endl;
            string choice{};
            cout << "Is this the line you want to delete[y/n]: ";
            cin >> choice;  
    
            if (choice =="y")
            {
                while(getline(database, line)){
                    if(line != var){
                        outputFile << line << endl;
                    }
                }
                database.close();
                outputFile.close();
                remove("database");
                rename("outputFileName","database");
            } else {
                continue;
            }
        } 
    }
    

    Think about what you're going to do. You've asked for the text of the line to delete. You're now reading lines.

    You read line 1. Let's say it doesn't match. You continue. You don't write line one.

    You read line 2. It matches, but let's say they say No when you ask to verify? You continue, but you don't write anything to your output file.

    This continues until you say Yes. At that point, you copy the remaining lines -- but you've ignored the ones you skipped.

    I think what you need to do is output non-matching lines to your output file.