My question is about file system or file handling.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shayaan Mustafa
    New Member
    • Dec 2010
    • 2

    #1

    My question is about file system or file handling.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<iostream.h>
    class employee{
          public:
    	int e_no;
    	float basic,deduction,allowence;
    	char name[50],address[50];
    	void add();//add new records.
    	void dis();//display records.
    };
    employee payroll;
    //********main()*********
    main(){
         clrscr();
         int choice;
         cout<<"==========Main Menu=========="<<endl;
         cout<<endl<<endl;
         cout<<"1-> Add Record"<<endl;
         cout<<"2-> Display Record"<<endl;
         cout<<"3-> Delete Record"<<endl;
         cout<<"4-> Update Record"<<endl;
         cout<<endl;
         cout<<"\n0-> Exit"<<endl;
         cout<<endl<<endl;
         cout<<"selection: ";cin>>choice;
         if(choice<0 || choice>4){
    	clrscr();
    	main();
         }
         switch(choice){
    	case 1:payroll.add(); break;
    	case 2:payroll.dis(); break;
    	case 0:exit(0);
         }
    getch();
    return 0;
    }
    void employee::add(){
         clrscr();
         FILE*fptr;
         int choice;
         employee e;
         do{
    	cout<<"========Enter New Record========"<<endl<<endl;
    	cout<<"Enter Employee Number: ";cin>>e_no;
    	cout<<"Enter Employee Name  : ";gets(e.name);
    	cout<<"Enter Address        : ";gets(e.address);
    	cout<<"Enter Basic Salary   : ";cin>>e.basic;
    	cout<<"Enter Allowence      : ";cin>>e.allowence;
    	cout<<"Enter Deduction      : ";cin>>e.deduction;
    	cout<<endl<<endl;
    	cout<<"1->Save | 2->Cancel"<<endl;cin>>choice;
          }while(!(choice==1 || choice==2));
    	if(choice==1){
    	   fptr=fopen("payroll.dat","w");
    	   fwrite(&e,sizeof(employee),1,fptr);
    	   fclose(fptr);
    	   cout<<"Record Saved"<<endl<<endl;
    	}else
    	   cout<<"Record Cancelled";
         cout<<"Enter Another Record? "<<endl;
         cout<<"1->Yes | 2->No"<<endl;cin>>choice;
    	if(choice==1)
    	   add();
    	else
    	   main();
    }
    void employee::dis(){
         FILE*fptr;
         employee e;
         fptr=fopen("payroll.dat","r+");
         while(fread(&e,sizeof(employee),1,fptr)){
    	  cout<<"Enter Employee Number To Display Record: ";cin>>e.e_no;
    	  cout<<"\nName         : "<<e.name;
    	  cout<<"\nAddress      : "<<e.address;
    	  cout<<"\nBasic Salary : "<<e.basic;
    	  cout<<"\nAllowence    : "<<e.allowence;
    	  cout<<"\nDeductions   : "<<e.deduction<<endl;
    	  cout<<endl;
    	  cout<<"\nTotal Salary :\t"<<e.basic+e.allowence;
    	  cout<<"\nNet Paid     :\t"<<e.basic+e.allowence-e.deduction;
         }
         fclose(fptr);
         main();
    }



    My problem is that when I enter all information and press 1 to save record, it is saved. But when I want to see that saved record it does not display.
    For example if I saves information for 2 employees. Employee number 1 and employee number 2.
    And I want to see information about employee number 2 then what should I do my code that it works well for me?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The problem is here:

    Code:
     main();
    Your record displays then the display function calls
    main() to restart your menu but main() has clrscr() as the first instruction so your display is erased before you can read it.

    Do not call main() like this.

    Call your add and display functions in the switch staement. You should have a loop in main() with the switch inside the loop. When the user chooses EXIT you return from an exit function which does a return fom main() and stops your program.

    Comment

    • Shayaan Mustafa
      New Member
      • Dec 2010
      • 2

      #3
      To weaknessforcats :

      Ok I got your idea. But Now there is still a problem that my content is overwritten means when I enter information of employee number 1 then it works well. But when I enter information for employee number 2 then information of employee#1 is overwritten by information of employee#2 and even I don't enter any more employee's information but they are also display with the information of employee#2. Why it is so? I have check "r" mode instead of "r+" but problem remains.

      And I want that if I want to see information of any employee I just enter its number and information is related to that employee is shown. How can I do that?
      Can you made changes in my code?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Every time you open the file you position the file to the point specified in the fopen. Either this is the beginning or the end.

        If it's the beginning, then employee 2 overlays employee 1.

        You need to:
        a) open the file once at the beginning of main().
        b) position the file to the end so that new data is written at the end of he file. You can do this in fopen or you can call seek() to the end of the file.
        c) to display an employee by name you would
        - position the file to the beginning
        - then drop into a loop that reads an employee from the file
        - check the name and if it is not the right one, read another employee. If it is the right one, return the employee
        - use the returned employee to cal your display function.

        Comment

        Working...