C++ File Handling!

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3066 Views - Last Post: 11 December 2012 - 08:16 PM Rate Topic: -----

#1 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

C++ File Handling!

Posted 09 December 2012 - 11:57 PM

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



Is This A Good Question/Topic? 0
  • +

Replies To: C++ File Handling!

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: C++ File Handling!

Posted 10 December 2012 - 02:14 AM

Moved to the C++ help forum. Please don't post questions in the Challenges sub-forums.
Was This Post Helpful? 0
  • +
  • -

#3 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: C++ File Handling!

Posted 10 December 2012 - 04:01 AM

To ease your job ,you should use a vector and C++ Strings .
Was This Post Helpful? 1
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: C++ File Handling!

Posted 10 December 2012 - 07:56 AM

eof in C++ streams does not mean the same as eof in C. I suggest you read This Tutorial first to understand this problem and the workarounds.

Basically, you should substitute char arrays with strings and use file1 >> name1 >> name2 >> address >> cell_Number >> land_line for both input operation and also condition checking.
Was This Post Helpful? 1
  • +
  • -

#5 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: C++ File Handling!

Posted 10 December 2012 - 09:09 AM

View PostAtli, on 10 December 2012 - 02:14 AM, said:

Moved to the C++ help forum. Please don't post questions in the Challenges sub-forums.

Sorry for this...!!!
Was This Post Helpful? 0
  • +
  • -

#6 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: C++ File Handling!

Posted 10 December 2012 - 09:16 AM

View PostTwoOfDiamonds, on 10 December 2012 - 04:01 AM, said:

To ease your job ,you should use a vector and C++ Strings .

I am restricted in my project to use only char arrays!!!
Was This Post Helpful? 0
  • +
  • -

#7 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: C++ File Handling!

Posted 10 December 2012 - 09:21 AM

View PostAnarion, on 10 December 2012 - 07:56 AM, said:

eof in C++ streams does not mean the same as eof in C. I suggest you read This Tutorial first to understand this problem and the workarounds.

Basically, you should substitute char arrays with strings and use file1 >> name1 >> name2 >> address >> cell_Number >> land_line for both input operation and also condition checking.

Thanks for such good..!!! Now, I understand where I did the mistake and I correct my code!!!
But, still I am facing a problem in comparing char arrays. i.e., suppose there are records of Khizar Iqbal and Uzair Iqbal, Then when I tried to delete the record of Khizar Iqbal, my program also remove the record of Uzair Iqbal. Means, it also delete the record of having whether the last_name and first_name or both..!!
Was This Post Helpful? 0
  • +
  • -

#8 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: C++ File Handling!

Posted 10 December 2012 - 10:16 AM

Take a look at this part of your code:
if (strcmp (name1, first_name) != 0 && strcmp(name2, last_name) != 0) {
    //rest of the code
}

Ok, let's go step by step. strcmp returns 0 if both strings are equal. Now tell me, what is being checked here? And also, what you wanted to do. Explain these and you can spot the problem :)
Was This Post Helpful? 0
  • +
  • -

#9 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: C++ File Handling!

Posted 10 December 2012 - 10:46 AM

View PostAnarion, on 10 December 2012 - 10:16 AM, said:

Take a look at this part of your code:
if (strcmp (name1, first_name) != 0 && strcmp(name2, last_name) != 0) {
    //rest of the code
}

Ok, let's go step by step. strcmp returns 0 if both strings are equal. Now tell me, what is being checked here? And also, what you wanted to do. Explain these and you can spot the problem :)/>/>/>/>/>

O.K., Suppose I have the following contacts list in Contacts.bat

Khizar Iqbal I-10 0111 0000
Uzair Iqbal I-11 0222 1111
Muhammed Zarar I-12 0333 2222
Muhammed Ahmed I-13 0444 3333


After execution my program, when I press 2, it prompt to enter the first name and then last name of the contact I want to delete the record.

When I am enter Khizar and Iqbal as first name and last name respectively, then it delete the record of both Khizar Iqbal and Uzair Iqbal (Both have the same last name)

When I again run the program and wants to delete the record of Muhammed Zarar, and give the input of Muhammed and Zarar as first name and last name respectively, then again the record of both Muhammed Zarar and Muhammed Ahmed are deleted (Both have the same first name).

If I give the inputs Muhammed and Iqbal as first name and last name respectively, then whole record is deleted, becase first two record has the Iqbal as last name and last two record has the Muhammed as first name!!!

As long as my understanding says, there is a problem in comparing with char arrays. i.e.,
if (strcmp (name1, first_name) != 0 && strcmp(name2, last_name) != 0)
{
   //do this
}


This post has been edited by Red Prince: 10 December 2012 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

#10 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: C++ File Handling!

Posted 10 December 2012 - 12:55 PM

The problem is a mismatch between the thing you want to do (what you expect your code to do), and the thing it actually does in the following part:
if (strcmp (name1, first_name) != 0 && strcmp(name2, last_name) != 0) {

What you have written here is that, if neither the first name nor the last name is equal to what you want to delete, you write this record to the temporary second file (which means you don't want to delete this record, which is correct). But, what if one of the first_name or last_name are equal to the corresponding value to be deleted? Just as you have stated, your code considers these kind of records to be deleted as well, which is wrong.

Let's consider all cases:
  • (name1 == first_name) and also (name2 == last_name) -> This record has to be deleted
  • (name1 != first_name) but (name2 == last_name) -> This record doesn't have to be deleted
  • (name1 == first_name) but (name2 != last_name) -> This record doesn't have to be deleted
  • (name1 != first_name) and also (name2 != last_name) -> This record doesn't have to be deleted

But your if statement deletes the record in the first three cases, instead of just the first case. So, change it in a way to match the above.
Was This Post Helpful? 2
  • +
  • -

#11 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: C++ File Handling!

Posted 11 December 2012 - 07:41 AM

@ Anarion
You are right. There is problem in in this line:
if (strcmp (name1, first_name) != 0 && strcmp(name2, last_name) != 0) {
    //rest of code



When I just replace && with || programs starts working as I want, but still I don't understand one thing that
if (strcmp (name1, first_name) != 0 && strcmp(name2, last_name) != 0) {
//rest of code
[/code]

means if namme == first_name and name2 == last_name, then this contact need not to be write on the new file. but why this is failed. If I use || instead of &&, then it means either name1 == first_name or name2 == last_name, then this record need not to be written on the file..!!! May be I misunderstand the problem or strcmp() function!!
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: C++ File Handling!

Posted 11 December 2012 - 08:02 AM

Quote

means if namme == first_name and name2 == last_name, then this contact need not to be write on the new file. but why this is failed

No you are saying if name is not equal to first name AND name2 is not equal to last name. Remember you used != 0 for your strcmp() comparison. This function returns zero when they are equal.

Maybe it would be easier to express this as:
if (!(strcmp (name1, first_name) == 0 && strcmp(name2, last_name) == 0))


This says if not (name1 is equal to first name AND name2 is equal to last name).

This is equivalent to:

if ((strcmp (name1, first_name) != 0 || strcmp(name2, last_name) != 0))


Which is if name1 is not equal to first name OR name2 is not equal to last name.

Sometimes I find it easier to deal in positives rather than negatives.

Jim

This post has been edited by jimblumberg: 11 December 2012 - 08:03 AM

Was This Post Helpful? 2
  • +
  • -

#13 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: C++ File Handling!

Posted 11 December 2012 - 08:18 AM

O.K., O.K,, now I understand what actually is happening!! Thanks for helping me!!
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2982
  • View blog
  • Posts: 10,299
  • Joined: 08-August 08

Re: C++ File Handling!

Posted 11 December 2012 - 08:22 AM

I think you're asking for trouble by using parallel arrays. Use one array of structs instead.
Sorry, I misread your code. You're not using an array of people at all. :ohno:

This post has been edited by CTphpnwb: 11 December 2012 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#15 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: C++ File Handling!

Posted 11 December 2012 - 08:34 AM

View PostCTphpnwb, on 11 December 2012 - 08:22 AM, said:

I think you're asking for trouble by using parallel arrays. Use one array of structs instead.
Sorry, I misread your code. You're not using an array of people at all. :ohno:/>

It's O.K., can you please share some good link about struct in c++!!! actually, I am working on a project and in the project we are restricted to use the char arrays, that's why I am using this!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2