This is the function
void erase(std::string ID)
{
std::ifstream inFile("userlist.txt");
std::ofstream outCopy("copy.txt");
std::string buffer;
bool copyDone = false;
while (getline(inFile, buffer))
{
if (buffer == ("ID: " + ID))
{
while (getline(inFile, buffer) && buffer != "ID: ");
copyDone = true;
buffer.clear();
std::cout << "ID Found. Deleting user.";
}
outCopy << buffer;
std::cout << buffer << std::endl;
}
if (copyDone == false)
{
std::cout << "ID not present in userlist.";
outCopy.close();
inFile.close();
remove("copy.txt");
return;
}
inFile.close();
outCopy.close();
inFile.open("copy.txt");
outCopy.open("userlist.txt", std::ios::trunc);
if (outCopy.good())
std::cout << "Hej.";
outCopy << inFile;
inFile.close();
outCopy.close();
//remove("copy.txt");
}
here is userlist.txt, where ID: 111d should be deleted
Hello.
ID: 111d
Test.
ID: 202d
Should be kept safe from harm.
The copy.txt where everything concerning ID: 111d should have been removed ,but not IDs after,
Hello.
The userlist.txt after erase(string) now only contain,
0x28fad4
I guess the read loop inside the first while loop needs another condition, and the way I try to copy input from inFile to inCopy do not work, but I thought it could have been interesting to know, if it was possible to copy from the copied text back again without getline().
Regards,
Boooke