I'm quite new to C++ programming.
I was trying to write a simple c++ to delete line from a text file but got some errors.
Any help will be appreciated.
This is the line:
int Transaction::delexpense()
{
string expitem, line;
cout << "Enter the expense you wish to delete:" << endl;
cin >> expitem;
ifstream in ("transaction.txt");
if (!in.is_open())
{
cout << "Record does not exists" << endl;
}
ofstream out ("temp.txt");
while (getline(in, line))
{
if ((line = line.find(expitem)))
out << line << "\n";
}
in.close();
out.close();
remove ("transaction.txt");
rename ("temp.txt", "transaction.txt");
}
I also found similar explanation over the Internet.
What I'm trying to do here is to delete the line input by user. So it is a string right?
How should I write it?
If you want to delete the line that contains string user entered somewhere in it, then write if( line.find( expitem ) == string::npos ) out << line << "\n";
.find returns the position of the found string or string::npos if it was not found.
If you want to delete a line that matches user input exactly, replace that condition with line != expitem