-2

Data.txt file:

John Smith Freshman U2010001
Kirti Seth Freshman U2010002
Nick Johnson Freshman U2010003
Alan Walker Freshman U2010004
Peter Foster Freshman U2010005

As soon as a user enters his/her ID, the program finds the line number, where the ID, that has been entered exists, and a new file NEWFILE.txt should store all lines except that line, but for some reason, my code is just storing the lines below the line, in which the user's ID exists, instead of storing all lines and skipping that line.

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

using namespace std;

int main()
{
string Delete_this_Line;
string line, Aline;
int LineNumber = 1, i=1;

ifstream Read("Data.txt", ios::in);
ofstream NewFile("NewFile.txt", ios::out | ios::app);

if (Read.is_open())
{
    cout << "ID (to be deleted): ";
    cin >> Delete_this_Line;
    while (!Read.eof()) 
    {
        getline(Read, line);
        size_t found = line.find(Delete_this_Line);
        if (found != string::npos)
        {               
            break;
        }
        LineNumber++;
    }
    while (!Read.eof()) // for each candidate word read from the file 
    {
        i++;
        getline(Read, Aline);
        NewFile << Aline << "\n";
        if (i == LineNumber)
        {
            continue;
        }
    }
}
Read.close();
NewFile.close();
return 0;
}

When I enter the ID U2010003, the compiler copies the following data into the NEWFILE.txt file:

NEWFILE.txt:

Alan Walker Freshman U2010004
Peter Foster Freshman U2010005

But I've expected to get the following result => NEWFILE.txt:

John Smith Freshman U2010001
Kirti Seth Freshman U2010002
Alan Walker Freshman U2010004
Peter Foster Freshman U2010005

I honestly don't know why this is happening, can you please help me to achieve the desired result?

Sam
  • 13
  • 4
  • 2
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/) – Remy Lebeau Aug 09 '21 at 20:05
  • 1
    When the code is looking for the ID, it reads a line and, if the ID doesn't match, it discards it. The first loop should write out the unmatched lines. – Pete Becker Aug 09 '21 at 20:08

1 Answers1

1

You are misusing the ifstream::eof() method. More importantly, you are using 2 separate loops, 1 loop to find the ID to delete, and 1 loop to copy all lines AFTER that ID, ignoring all lines BEFORE the ID. Using 1 loop for everything will suffice instead, eg:

#include <iostream>
#include <stream>
#include <string>

using namespace std;

int main()
{
    string Delete_this_Line, line;

    ifstream Read("Data.txt");
    if (Read.is_open())
    {
        ofstream NewFile("NewFile.txt");
        if (NewFile.is_open())
        {
            cout << "ID (to be deleted): ";
            cin >> Delete_this_Line;

            while (getline(Read, line)) 
            {
                if (line.find(Delete_this_Line) == string::npos)
                    NewFile << line << "\n";
            }
        }
    }
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770