I'm trying to create a to-do list program.
I've got a problem removing a task from the txt file, I've tried to read every data from the program and replace that certain data with blank, but in the end, there are still some characters that are left behind.
bool DoList::Remove_Task()
{
std::ofstream temp_file("temp.txt", std::ios::app);
std::ifstream file("Tasks_Data.txt");
if (!temp_file.is_open())
{
std::cerr << "temp File cannot be opened to modify";
return false;
}
else if (!file.is_open())
{
std::cerr << "File cannot be opened to read";
return false;
}
else
{
int ID;
int search;
std::string TaskName;
std::string Description;
std::string Status;
char str[26];
std::string line;
std::cout << "Enter Your Task ID to remove: ";
std::cin >> search;
while (getline(file, line))
{
while(file >> ID)
if (ID == search)
{
ID = 0;
TaskName = "\0";
Description = "\0";
Status = "\0";
strcpy_s(str, "\0");
temp_file << ID << ". " << std::setprecision(5) << TaskName << ", " << std::setprecision(5) << Description << ", " << std::setprecision(5) << Status << ", " << std::setprecision(5) << str << std::endl;
}
else
{
temp_file << line;
}
}
}
file.close();
temp_file.close();
remove("Tasks_Data.txt");
rename("temp.txt", "Tasks_Data.txt");
return true;
}