Hello,
I finished writing a Phonebook application today, I just wanted to practice dealing with writing/reading/handling data from external files. All went well and the application works just like I want it too, but I was just wondering if my implementation is "the correct" way of for deleting specific portions of txt files. I wrote the function 2 different ways, one by creating a vector filled with Person objects that contains info from the contact list and I use those to fill the contact list file and the other way (which is commented out) was done by writing to a tmp txt file with the data minus what I want to delete and write over the original file with whats in the tmp file. I didn't like this way because I had the tmp contact file list lingering around.
Any advice on the proper way of handling this type of task or if you know a better way please let me know! Thanks!
void Phonebook::deleteEntry()
{
int selection;
bool valid = false;
do
{
displayAllEntries();
if (num_of_contacts < 1)
break;
std::cout << "\n\n\t\t\t\t\tEnter in the Entry to delete (0 = Cancel): ";
std::cin >> selection;
valid = std::cin.good();
// making sure the selection was an int value
if (!valid)
{
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
}
else
{
// checks if user has contacts to delete and if the selection
// is in range
if ((selection <= num_of_contacts) && (num_of_contacts > 0) && (selection != 0))
{
std::vector<Person> contacts;
std::ifstream reader("ContactList.txt", std::ios::in);
if (reader.is_open())
{
// read data from ContactList and create Persons
// to fill the fill the file after deletion
// ignores the selected contact
std::string fname, lname, email, phone_number;
for (int i = 0; i < num_of_contacts; i++)
{
reader >> fname >> lname >> email >> phone_number;
// i is always 1 less than the actual selection number
if ((i + 1) != selection)
contacts.push_back(Person(fname, lname, email, phone_number));
}
num_of_contacts--;
reader.close();
}
else
{
std::cout << "\n\n\t\t\t\t\t*** [ Error! Cannot Open Contact List ] ***";
std::cout << "\n\t\t\t\t\t"; system("PAUSE");
}
std::ofstream writer("ContactList.txt", std::ios::out);
if(writer.is_open())
{
// Fills the file with the data from the contact vector
for (int i = 0; i < num_of_contacts; i++)
{
writer << contacts[i].getFirstName() << " "
<< contacts[i].getLastName() << " "
<< contacts[i].getEmail() << " "
<< contacts[i].getNumber() << std::endl;
}
std::cout << "\n\n\t\t\t\t\t----------------------------";
std::cout << "\n\t\t\t\t\t| Deletion Successfull |";
std::cout << "\n\t\t\t\t\t----------------------------";
writer.close();
}
else
{
std::cout << "\n\n\t\t\t\t\t*** [ Error! Cannot Open Contact List ] ***";
std::cout << "\n\t\t\t\t\t"; system("PAUSE");
}
// FILE READING/WITING TO TMP FILE IMPLEMENTATION
/*std::ifstream reader("ContactList.txt", std::ios::in);
std::ofstream writer("tmp_contacts.txt", std::ios::out);
if (reader.is_open())
{
std::string fname, lname, email, phone_num;
for (int i = 1; i <= num_of_contacts; i++)
{
if (i != selection)
{
reader >> fname >> lname >> email >> phone_num;
writer << fname << " "
<< lname << " "
<< email << " "
<< phone_num << std::endl;
}
else
{
reader >> fname >> lname >> email >> phone_num;
}
}
num_of_contacts -= 1;
reader.close();
writer.close();
writer.open("ContactList.txt", std::ios::out);
reader.open("tmp_contacts.txt", std::ios::in);
for (int i = 1; i <= num_of_contacts; i++)
{
reader >> fname >> lname >> email >> phone_num;
writer << fname << " "
<< lname << " "
<< email << " "
<< phone_num << std::endl;
}
std::cout << "\n\n\t\t\t\t\t----------------------------";
std::cout << "\n\t\t\t\t\t| Deletion Successfull |";
std::cout << "\n\t\t\t\t\t----------------------------";
}
else
{
std::cout << "\n\n\t\t\t\t\t*** [ Error! Cannot Open Contact List ] ***";
std::cout << "\n\t\t\t\t\t"; system("PAUSE");
}*/
}
else if (selection == 0)
{
std::cout << "\n\n\t\t\t\t\t-----------------------------------------";
std::cout << "\n\t\t\t\t\t| Canceling Request To Delete Entry |";
std::cout << "\n\t\t\t\t\t-----------------------------------------";
}
else
{
valid = false;
std::cout << "\n\n\t\t\t\t\t*** [ Error! Invalid Selection ] ***";
std::cout << "\n\t\t\t\t\t"; system("PAUSE");
}
}
} while ((!valid) && (num_of_contacts != 0));
}