C(file handling problem)

This is a discussion on C(file handling problem) within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    C(file handling problem)

    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");
    	   fprintf(fptr,"%d\n",e_no);
    	   fprintf(fptr,"%s\n",e.name);
    	   fprintf(fptr,"%s\n",e.address);
    	   fprintf(fptr,"%f\n",e.basic);
    	   fprintf(fptr,"%f\n",e.allowence);
    	   fprintf(fptr,"%f\n",e.deduction);
    	   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(){
         int num;
         FILE*fptr;
         employee e;
         cout<<"Enter Employee Number: ";cin>>num;
         if(num==e_no){
         fptr=fopen("payroll.dat","r+");
         fscanf(fptr,"%d\n",&e_no);
         fscanf(fptr,"%s\n",&e.name);
         fscanf(fptr,"%s\n",&e.address);
         fscanf(fptr,"%f\n",&e.basic);
         fscanf(fptr,"%f\n",&e.allowence);
         fscanf(fptr,"%f\n",&e.deduction);
         cout<<"\nNumber       :"<<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);
         }
         else
         cout<<"wrong";
    }


    Here is my code. My problem is that when I enter information of an employee and then without exiting output screen I see saved record of that employee then program works properly.
    But when I quit to program and again compile and run program and enters a employee number for which I want to see a record then it doesn't show any program but shows main menu. Is my file doesn't save properly?
    What is problem with my code?
    And secondly if I wish to add multiple employee record and want to see individual information by given their employee number then program must show only that employee's information for which I have entered employee number.
    I hope you understand my question. Help me please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1) If this is C, why did you post in the C++ forum?

    B) When you open the file in "w" mode, then all previous information in that file vanishes. So if you wanted to add more than one employee, well, that would be a problem.

    iii) You have no way of setting e_no before doing the if(num==e_no) decision in your "dis" function, so you have a 1/sizeof(int) chance of that working, depending on whether e_no for that particular employee object happens to match the typed-in number or not.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by tabstop View Post
    1) If this is C, why did you post in the C++ forum?

    B) When you open the file in "w" mode, then all previous information in that file vanishes. So if you wanted to add more than one employee, well, that would be a problem.

    iii) You have no way of setting e_no before doing the if(num==e_no) decision in your "dis" function, so you have a 1/sizeof(int) chance of that working, depending on whether e_no for that particular employee object happens to match the typed-in number or not.
    1)I don't know it is C or C++. Some say it is C++ some say C. Sorry.

    B) Why it is so when I open file in "w" mode? What mode should I use that information remain save until I delete it by myself?

    iii)So if I use if(1/sizeof(int)) instead of if(num==e_no) then it should be working, right?
    If I am wrong then how I will be able to see my desired employee's information by providing employee's number of that employee?

    KINDLY HELP ME. I have to submit it to Monday. Please.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1) What you've written is C+-, I guess. If you're using C++, chuck FILE, fscanf, fprintf and say hello to your new friend fstream.

    1.25) I didn't notice before, but it's not possible to re-read your file anyway, assuming people have spaces in their names and addresses -- you'll never know where one stops and the next one starts, let alone the fact that you can't read anything with spaces in it using fscanf+%s, or for that matter >>.

    1.5) So really your first order of business is to figure out a sane file format. You can go old-school (as in 1960s old school) and do fixed-format records, where columns 1-20 are the name, columns 21-60 is the address, etc. Or you can put in delimiters (a symbol that won't appear in a valid name or address -- comma might work, depending on what addresses look like in your part of the world) to separate fields.

    B) If you want to append, you need to be in "a" mode. Although again you shouldn't be using FILE. You can make an fstream, which is in/out mode (as opposed to an ifstream, which is in, or an ofstream, which is out).

    iii) If you want to know what the employee number is that is stored in the file, you'll have to read it in. You can't check whether the number they typed in matches the employee number of the file before you read it in, only after.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by tabstop View Post
    1) What you've written is C+-, I guess. If you're using C++, chuck FILE, fscanf, fprintf and say hello to your new friend fstream.

    1.25) I didn't notice before, but it's not possible to re-read your file anyway, assuming people have spaces in their names and addresses -- you'll never know where one stops and the next one starts, let alone the fact that you can't read anything with spaces in it using fscanf+%s, or for that matter >>.

    1.5) So really your first order of business is to figure out a sane file format. You can go old-school (as in 1960s old school) and do fixed-format records, where columns 1-20 are the name, columns 21-60 is the address, etc. Or you can put in delimiters (a symbol that won't appear in a valid name or address -- comma might work, depending on what addresses look like in your part of the world) to separate fields.

    B) If you want to append, you need to be in "a" mode. Although again you shouldn't be using FILE. You can make an fstream, which is in/out mode (as opposed to an ifstream, which is in, or an ofstream, which is out).

    iii) If you want to know what the employee number is that is stored in the file, you'll have to read it in. You can't check whether the number they typed in matches the employee number of the file before you read it in, only after.
    I really don't understand what are you talking about?
    Can you make any implementation in my code?
    Can change my code and re post it again?
    I am basically writing a program of payroll of an employee.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Depends on what you mean by "implemention in [your] code". I can see approximately 15 lines that you won't have to get rid of. Calling main() inside main()? Tacky tacky tacky. Using C strings instead of std::strings? Bad idea. <conio.h>? Please.

    And then there's your class design. Your employee class has data fields (like employee number, basic salary, etc.) for a single person, which is great. Having a display function is reasonable ... but that should display the employee under consideration -- it certainly shouldn't go out looking for some employee somewhere in some file. You already are an employee! And the add function is just, no. Add to what? There's just an employee; what are you trying to add it to? Some of that should go in the constructor of the class. But in any case writing to a file is presumptuous. What you need is to fix your employee class a bit so that it deals with one single employee. Then your main program can have a container full of employees: an array, a vector, a list, a set, whatever you want. But the employees themselves are just unitary objects. You can then worry about writing/reading the container of employees to/from a file at that point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Problem while dealing with file handling functions
    By RoshanGautam in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 01:42 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. File Handling Problem
    By Erkan in forum C Programming
    Replies: 4
    Last Post: 10-25-2005, 07:29 AM
  5. mixer to sound card problem, need help ASAP
    By Waldo2k2 in forum Tech Board
    Replies: 0
    Last Post: 12-09-2002, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21