Doing File operations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tracethepath
    New Member
    • Jun 2007
    • 15

    Doing File operations

    i am new to this...
    i was making a menu driven program on file operations..
    to add, delete, modify, search and display a particular record and list al the records...
    i have not yet written function modify...
    but the rest of the program shuld work properly but its not...
    its not doing searching by name properly and that deleting records...
    heres d code:
    [CODE=cpp] class example
    {
    public:

    char name[10];
    int number;

    void getdetails()
    {
    cout<<"Enter the name"<
    gets(name);
    cout<<"Enter the number"<
    cin>>number;
    }

    void showdetails()
    {
    cout<<"NAME: "<<<<"NUMBE R: "<<
    }

    };

    void add()
    {
    example abc;
    abc.getdetails( );
    ofstream file1("EXAMPLE. dat", ios::binary|ios ::app);
    file1.write((ch ar*)&abc, sizeof(abc));
    file1.close();
    }

    void del()
    {
    int choice;
    char name[10];
    int number;
    example xyz;
    cout<<"Do you wish to search and delete according to name(1) or

    number(2)"<
    cin>>choice;

    switch(choice)

    {
    case 1:
    cout<<"Enter the name"<
    gets(name);

    fstream file3, file2;
    file3.open("EXA MPLE.dat", ios::binary);
    file2.open("tem p.dat", ios::binary);

    while(file3)
    {
    file3.read((cha r*)&xyz, sizeof(xyz));
    if(!(abc.name== name))
    file2.write((ch ar*)&xyz, sizeof(xyz));
    }

    file3.close();
    file2.close();
    remove("EXAMPLE .dat");
    rename("temp.da t", "EXAMPLE.da t");
    break;

    case 2:
    cout<<"Enter the number"<
    cin>>number;

    fstream file4, file5;
    file4.open("EXA MPLE.dat", ios::binary);
    file5.open("tem p.dat", ios::binary);

    while(file4)
    {
    file4.read((cha r*)&xyz, sizeof(xyz));
    if(!(abc.number ==number))
    file5.write((ch ar*)&xyz, sizeof(xyz));
    }

    file4.close();
    file5.close();
    remove("EXAMPLE .dat");
    rename("temp.da t", "EXAMPLE.da t");
    break;

    default:
    cout<<"wrong choice"<
    break;
    }
    getch();
    }

    void modify()
    {

    }

    void particular()
    {
    int choice;
    char name[10];
    int number;
    example abc;
    cout<<"Do you wish to search and display according to name(1) or

    number(2)"<
    cin>>choice;

    switch(choice)

    {
    case 1:
    cout<<"Enter the name"<
    gets(name);
    ifstream file6("EXAMPLE. dat", ios::binary);

    while(file6)
    {
    file6.read((cha r*)&abc, sizeof(abc));
    if(abc.name==na me)
    abc.showdetails ();
    }

    file6.close();
    break;

    case 2:
    cout<<"Enter the number"<
    cin>>number;
    ifstream file7("EXAMPLE. dat", ios::binary);

    while(file7)
    {
    file7.read((cha r*)&abc, sizeof(abc));
    if(abc.number== number)
    abc.showdetails ();
    }

    file7.close();
    break;

    default:
    cout<<"wrong choice"<
    break;
    }
    getch();
    }

    void listall()
    {
    example abc;
    ifstream file8("EXAMPLE. dat", ios::binary);

    while(file8)
    {
    file8.read((cha r*)&abc, sizeof(abc));
    abc.showdetails ();
    cout<
    }

    file8.close();
    getch();
    }

    void main()
    {

    char ch='y';
    while(ch=='y')
    {

    clrscr();
    int choice;
    cout<<"MENU:"<
    cout<<"1. Add a record"<
    cout<<"2. Delete a record"<
    cout<<"3. Modify a record"<
    cout<<"4. Search and display a particuar record"<
    cout<<"5. List all the records"<
    cout<<"6. Quit"<
    cout<
    cout<<"enter your choice"<
    cin>>choice;

    switch(choice)

    {
    case 1:
    add();
    break;

    case 2:
    del();
    break;

    case 3:
    modify();
    break;

    case 4:
    particular();
    break;

    case 5:
    listall();
    break;

    case 6:
    exit(0);
    }

    }

    getch();
    }[/CODE]

    please help me out....
    Last edited by r035198x; Jun 29 '07, 08:48 AM. Reason: added the missing code tags
  • tracethepath
    New Member
    • Jun 2007
    • 15

    #2
    i am new to this...
    i was making a menu driven program on file operations..
    to add, delete, modify, search and display a particular record and list al the records...
    i have not yet written function modify...
    but the rest of the program shuld work properly but its not...
    its not doing searching by name properly and that deleting records...
    heres d code:
    [CODE=cpp] class example
    {
    public:

    char name[10];
    int number;

    void getdetails()
    {
    cout<<"Enter the name"<
    gets(name);
    cout<<"Enter the number"<
    cin>>number;
    }

    void showdetails()
    {
    cout<<"NAME: "<<<<"NUMBE R: "<<
    }

    };

    void add()
    {
    example abc;
    abc.getdetails( );
    ofstream file1("EXAMPLE. dat", ios::binary|ios ::app);
    file1.write((ch ar*)&abc, sizeof(abc));
    file1.close();
    }

    void del()
    {
    int choice;
    char name[10];
    int number;
    example xyz;
    cout<<"Do you wish to search and delete according to name(1) or

    number(2)"<
    cin>>choice;

    switch(choice)

    {
    case 1:
    cout<<"Enter the name"<
    gets(name);

    fstream file3, file2;
    file3.open("EXA MPLE.dat", ios::binary);
    file2.open("tem p.dat", ios::binary);

    while(file3)
    {
    file3.read((cha r*)&xyz, sizeof(xyz));
    if(!(abc.name== name))
    file2.write((ch ar*)&xyz, sizeof(xyz));
    }

    file3.close();
    file2.close();
    remove("EXAMPLE .dat");
    rename("temp.da t", "EXAMPLE.da t");
    break;

    case 2:
    cout<<"Enter the number"<
    cin>>number;

    fstream file4, file5;
    file4.open("EXA MPLE.dat", ios::binary);
    file5.open("tem p.dat", ios::binary);

    while(file4)
    {
    file4.read((cha r*)&xyz, sizeof(xyz));
    if(!(abc.number ==number))
    file5.write((ch ar*)&xyz, sizeof(xyz));
    }

    file4.close();
    file5.close();
    remove("EXAMPLE .dat");
    rename("temp.da t", "EXAMPLE.da t");
    break;

    default:
    cout<<"wrong choice"<
    break;
    }
    getch();
    }

    void modify()
    {

    }

    void particular()
    {
    int choice;
    char name[10];
    int number;
    example abc;
    cout<<"Do you wish to search and display according to name(1) or

    number(2)"<
    cin>>choice;

    switch(choice)

    {
    case 1:
    cout<<"Enter the name"<
    gets(name);
    ifstream file6("EXAMPLE. dat", ios::binary);

    while(file6)
    {
    file6.read((cha r*)&abc, sizeof(abc));
    if(abc.name==na me)
    abc.showdetails ();
    }

    file6.close();
    break;

    case 2:
    cout<<"Enter the number"<
    cin>>number;
    ifstream file7("EXAMPLE. dat", ios::binary);

    while(file7)
    {
    file7.read((cha r*)&abc, sizeof(abc));
    if(abc.number== number)
    abc.showdetails ();
    }

    file7.close();
    break;

    default:
    cout<<"wrong choice"<
    break;
    }
    getch();
    }

    void listall()
    {
    example abc;
    ifstream file8("EXAMPLE. dat", ios::binary);

    while(file8)
    {
    file8.read((cha r*)&abc, sizeof(abc));
    abc.showdetails ();
    cout<
    }

    file8.close();
    getch();
    }

    void main()
    {

    char ch='y';
    while(ch=='y')
    {

    clrscr();
    int choice;
    cout<<"MENU:"<
    cout<<"1. Add a record"<
    cout<<"2. Delete a record"<
    cout<<"3. Modify a record"<
    cout<<"4. Search and display a particuar record"<
    cout<<"5. List all the records"<
    cout<<"6. Quit"<
    cout<
    cout<<"enter your choice"<
    cin>>choice;

    switch(choice)

    {
    case 1:
    add();
    break;

    case 2:
    del();
    break;

    case 3:
    modify();
    break;

    case 4:
    particular();
    break;

    case 5:
    listall();
    break;

    case 6:
    exit(0);
    }

    }

    getch();
    }[/CODE]

    please help me out....
    pls... help me..

    Comment

    • niskin
      New Member
      • Apr 2007
      • 109

      #3
      Which aspect do you want us to help you with? Do you know which lines are not compiling, or if it is compiling what is wrong?

      Comment

      • tracethepath
        New Member
        • Jun 2007
        • 15

        #4
        Originally posted by niskin
        Which aspect do you want us to help you with? Do you know which lines are not compiling, or if it is compiling what is wrong?
        the function void delete is not working properly...It deletes all the data...and the function add, it adds the last entry twice..thanx 4 ur concern...

        Comment

        Working...