How would I be able to delete a line above a string? For example i'm opening a text file and editing it. The way my program works is the user inputs a teachers name. "Mr. John Doe" Then it deletes the next 7 lines from the name searched. Looks for everything in between "Courses" and "endInstructor" and deletes it, but what is left is the two strings above the name that was searched which is "loginpw123" and "startInstructor".
Now my question is how would I delete those two lines after everything else is removed, because it's different for every professor and the list has about 20 profiles on it.
Here is the code:
int main()
{
ifstream myfile("instructor.csv");
ofstream myfile2("outfile.csv");
string line;
string dropInstructor;
bool notFound = false;
cout << "Enter Instructor to drop by name: ";
getline(cin, dropInstructor);
while (getline(myfile, line))
{
if (line != dropInstructor)
{
myfile2 << line << endl;
}
if (line == dropInstructor)
{
myfile2 << "";
for (int i = 0; i < 7; i++)
{
getline(myfile, line);
myfile2 << "";
}
getline(myfile, line);
if (line == "Courses:")
{
myfile2 << "";
while (getline(myfile, line))
{
if (line == "endInstructor")
{
myfile2 << "";
break;
}
myfile2 << "tempp";
}
/*for (int i = 0; i < 4; i++)
{
getline(myfile, line);
myfile2 << "";
}*/
}
}
}
/*if (!notFound)
{
cout << "Not found" << endl;
cin.get();
// client ->sendToClient("Not Found");
}*/
myfile.close();
myfile2.close();
remove("instructor.csv");
rename("outfile.csv", "instructor.csv");
}
text file:
loginpw123
startInstructor
Mr. John Doe
755 Teacher St
532 791 3761
[email protected]
392817
03/02/1988
Male
777777
Courses:
Courseinfo1
Courseinfo2
Courseinfo3
Courseinfo4
Courseinfo5
endInstructor
You are copying the first two lines to the new file before you have determined whether or not they belong to the professor that is being removed (why is the line above startProfessor
not between startProfessor
/endProfessor
instead?). You need to save the first line in memory temporarily, then check the professor, and if not a match then write all lines related to the professor to the new file.
Try something like this:
int main()
{
ifstream myfile("instructor.csv");
ofstream myfile2("outfile.csv");
string line, lineAboveProfessor;
string dropInstructor;
bool ignore = false, found = false;
cout << "Enter Instructor to drop by name: ";
getline(cin, dropInstructor);
while (getline(myfile, line))
{
if (line == "startInstructor")
{
if (!getline(myfile, line))
break;
ignore = (line == dropProfessor);
if (ignore)
{
found = true;
}
else
{
myfile2 << lineAboveProfessor << endl
<< "startInstructor" << endl
<< line << endl;
}
while (getline(myfile, line))
{
if (!ignore)
myfile2 << line << endl;
if (line == "endProfessor")
{
if (!ignore)
myfile2 << endl;
break;
}
}
lineAboveProfessor = "";
}
else
{
lineAboveProfessor = line;
}
}
/*if (!found)
{
cout << "Not found" << endl;
cin.get();
// client ->sendToClient("Not Found");
}*/
if (myfile1.eof() && myfile2.good())
{
myfile.close();
myfile2.close();
remove("instructor.csv");
rename("outfile.csv", "instructor.csv");
}
else
{
myfile.close();
myfile2.close();
remove("outfile.csv");
}
}