I tried to remove a single line from a file using user input and ended up removing all the lines.
/*inside of the file looks like this //courseName_c.str() file
1. Intro to C Programming
2. Data Type // this is the one i want to remove
3. Function
*/
void Topic::removeTopic() {
Course CO;
string courseName = "";
string topicName = "";
string content = "";
cout<<"Remove Topic" << endl;
ifstream topiclist(courseName.c_str()); /*courseName contains the list of the topic from which i intend to delete a specific line using user input but ending up deleting all the line in it */
while(getline(topiclist, content)) {
std::cout << content << endl ;
}
cout << "Type the Topic Name you want to Remove: " ;
getline(cin, topicName);
ofstream temp;
temp.open("temp");
while(getline(topiclist, content)) {
content.replace(content.find(content),content.length(),"");
temp << content << endl;
}
/*while (getline(topiclist, content))
{
if (content != topicName) //i also tried like this
{
temp << content << endl;
}
}
*/
topicist.close();
temp.close();
remove(courseName.c_str());
rename("temp", courseName.c_str());
// this part is for removing the associating file and it works perfectly
if (remove(topicName.c_str( )) !=0)
cout<<"\n\nRemove operation failed"<<endl;
else
cout<< topicName<<" has been removed."<<endl;
} `