Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

C++ File Handling!

$
0
0
Hello, every one there, I am stuck on a problem..!! Actually, I tried to make a Telephone Dictionary and for this I use the following code, when I am inserting a contact it successfully do this. But when I want to delete a contact then I am not get the expected result.!!!
Suppose, in my contacts.bat file, the following contact list is written:

Anus Mehmood I-10 0567 1244
Khizar Iqbal I-14 0333 068
Zarar Haider I-8 0456 456
Uzair Iqbal I-15 1234 5678
Naqash Mehmood I-16 1234 5678

and I want to delete the contact having first name Khizar and second name Iqbal, then my program do this but after it gives me the following result..!!

Anus Mehmood I-10 0567 1244
Zarar Haider I-8 0456 456
Naqash Mehmood I-16 1234 5678
Naqash Mehmood I-16 1234 5678

i.e., Uzair Iqbal is also removed but Naqash Mehmood is written for the two times!!
and after this if I want to remove the Anus Mehmood contact, them Naqash Mehmood contact is also removed..!!!
As long as I know, the is an error in the following line:
if (strcmp (name1, first_name) != 0 && strcmp(name2, last_name) != 0)
ANY BODY CAN HELP ME, PLEASE...!!! HERE IS MY CODE..!!!
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

using namespace std;

void delete_record(char first_name[], int first_length, char last_name[], int last_length)
{
    ifstream file1;
    file1.open("C:\\Users\\Red Prince!\\Desktop\\Contacts.dat");
    fstream file2;
    file2.open("C:\\Users\\Red Prince!\\Desktop\\Contacts2.dat" , ios::app | ios::out);

    while (!file1.eof())
    {
        char name1[15];
        char name2[15];
        char address[30];
        char cell_Number[13];
        char land_line[15];

        file1 >> name1 >> name2 >> address >> cell_Number >> land_line;

        if (strcmp (name1, first_name) != 0 && strcmp(name2, last_name) != 0)
         {
                 file2 << name1 <<  " " <<  name2 << " " << address << " " << cell_Number << " " << land_line << endl;
         }
    }

    file1.close();
    file2.close();

    remove("C:\\Users\\Red Prince!\\Desktop\\Contacts.dat"); // Delte the orignal file..!!
    rename("C:\\Users\\Red Prince!\\Desktop\\Contacts2.dat", "C:\\Users\\Red Prince!\\Desktop\\Contacts.dat");
}

void get_input(char input[], int length)
{
    cin.getline(input, length);
    cin.clear();
    //cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

void insert_contact()
{
    cin.ignore();
    cout << "Please Enter Your First Name: ";
    char first_name[15];
    get_input(first_name, 15);

    cout << "Please Enter Your Last Name: ";
    char last_name[15];
    get_input(last_name, 15);

    cout << "Please Enter Your Address (Max 30 Characters Long): ";
    char address[30];
    get_input(address, 30);

    cout << "Please Enter Your Cell Number: ";
    char cell_Number[13];
    get_input(cell_Number, 13);

    cout << "Please Enter Your Land Line Number: ";
    char land_line[15];
    get_input(land_line, 15);

    fstream file;
    file.open("C:\\Users\\Red Prince!\\Desktop\\Contacts.dat", ios::app | ios::in | ios::out);
    file << first_name << " " <<  last_name << " " << address << " " << cell_Number << " " << land_line << endl;
    file.close();
}

int main()
{
    cout << "Please Enter a choise: \n"
             << "1. Insert a contact\n"
             << "2. Delete a contact\n"
             << "3. Edit an existing contact (changing or modifying a record)"
             << "4. Search a contact (and display complete record on screen)\n"
             << "\ta. Search through first name\n"
             << "\tb. Search through last name\n"
             << "\tc. Search through landline\n"
             << "\td. Search through cell number\n"
             << "5. Display all contacts that\n"
             << "\ta. Start with a given letter (e.g., all contacts starting with ‘M’)\n"
             << "\tb. Landline or cell number starting with a particular pattern (e.g., 0300 for cell number, or (042)3518 for landline number)\n"
             << "6. Sort all contacts with respect to\n"
             << "\ta. first name\n"
             << "\tb. last name\n"
             << "\tc. land line number\n"
             << "\td. mobile number\n"
             << "7. Exit\n";

    int choise;
    cin >> choise;

    switch(choise)
    {
        case 1:
            insert_contact();
            break;

        case 2:
            {
                cout << "Please enter first name of the contact: ";
                char first_name[15];
                cin >> first_name;

                cout << "Please enter second name of the contact: ";
                char last_name[15];
                cin >> last_name;

                delete_record(first_name, 15, last_name, 15);
            }

    }

    return 0;
}
//Khizar Iqbal I-14 0333 068
//Anus Mehmood I-8 0332 062


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>