help regarding file handling...

how can save/retrieve/edit/delete the employee's data??

help me please...T_T..

here's my draft codes...

#include
#include
#include



struct person_info{
char employee[15],address[33];
int emp_id,age;
}empl;

struct payroll{
float basic,cola,gp,netpay;
float sss,love,med,tax,totald;
int rate,day;
}pay;


main(void){
clrscr();
char str[10];
int choice;
cout<<"M E N U"<<endl;
cout<<"[1]add employee"<<endl;
cout<<"[2]show information"<<endl;
cout<<"[3]exit"<<endl;
cout<<"E N T E R C H O I C E:";
cin>>choice;
switch(choice){
case 1:
clrscr();
cout<<"Employee Information"<<endl;
cout<<"enter employee:";
cin>>empl.employee;
cout<<"enter address:";
cin>>empl.address;
cout<<"enter age:";
cin>>empl.age;
cout<<"enter employee's ID:";
cin>>empl.emp_id;
cout<<"payroll entry"<<endl;
cout<<"enter rate:";
cin>>pay.rate;
cout<<"enter day:";
cin>>pay.day;
getche();main();
break;
case 2:
cout<<"display Information:"<<endl;
cout<<"employee name:"<<empl.employee<<endl;
cout<<"employee address:"<<empl.address<<endl;
cout<<"employee ID:"<<empl.emp_id<<endl;
cout<<"employee age:"<<empl.age<<endl;
cout<<"payslip information:"<<endl;
cout<<"rate per day:"<<pay.rate<<endl;
cout<<"number of days work:"<<pay.day<<endl;
getche();
main();
break;
case 3:
exit(0);break;
default:
cout<<"Invalid input";
getche();
main();
}
getche();
}

hope someone will help me...T_T..

thx..
«1

Comments

  • [color=Blue]
    Your code only deals with a single item. Each next item you enter will override the previous one. To resolve this you need an array of items:
    [/color]
    [code]
    typedef struct
    {
    ...
    }
    EMPLOYEE;

    EMPLOYEE ArrayOfPeople [32]; // 32 persons stored
    int NumArrayOfPeople;
    [/code]
    [color=Blue]
    When you add employees - you need to append each structure at the end of this array:
    [/color]
    [code]
    void function_add_employee ()
    {
    EMPLOYEE employee;

    //... fill 'employee' structure with input from cin.

    ArrayOfPeople [NumArrayOfPeople++] = employee;
    }
    [/code]
    [color=Blue]
    Call this function in response to the command to add employee. To load/save this array into a file use following:
    [/color]
    [code]
    void function_save ()
    {
    FILE* f = fopen ("", "wb");
    if (f == NULL) return;

    fwrite (&NumArrayOfPeople, sizeof (int), 1, f);
    fwrite (ArrayOfPeople, sizeof (EMPLOYEE), NumArrayOfPeople, f);
    fclose (f);
    }

    void function_load ()
    {
    FILE* f = fopen ("", "rb");
    if (f == NULL) return;

    fread (&NumArrayOfPeople, sizeof (int), 1, f);
    fread (ArrayOfPeople, sizeof (EMPLOYEE), NumArrayOfPeople, f);
    fclose (f);
    }
    [/code]
    [color=Blue]
    And why you calling main() from main()? Not a good idea. Use a while() loop instead. Turbo C has a small stack by default - you can run out easily if your code runs long enough.

    To display the list of employees - you need to recode it a little - make a loop where you will be using index to access the next person from the array and print that info.
    [/color]
  • can you please explain further because I don't know exactly how to apply the codes that you've given...

    I know there's terribly wrong on what i did with your codes...



    #include
    #include
    #include
    #include




    typedef struct
    {
    char employee[15],address[33];
    int emp_id,age;
    }
    EMPLOYEE;

    EMPLOYEE ArrayOfPeople [32]; // 32 persons stored
    int NumArrayOfPeople;


    struct payroll{
    float basic,cola,gp,netpay;
    float sss,love,med,tax,totald;
    int rate,day;
    }pay;


    void function_add_employee ();
    void function_save ();
    void function_load ();


    main(void){
    clrscr();
    char str[10];
    int choice;
    cout<<"M E N U"<<endl;
    cout<<"[1]add employee"<<endl;
    cout<<"[2]show information"<<endl;
    cout<<"[3]exit"<<endl;
    cout<<"E N T E R C H O I C E:";
    cin>>choice;
    switch(choice){
    case 1:
    clrscr();
    function_add_employee ();
    getche();main();
    break;
    case 2:
    clrscr();
    function_load ();
    getche();
    main();
    break;
    case 3:
    clrscr();
    function_save ();
    getche();
    break;
    case 4:exit(0);
    break;
    default:
    cout<<"Invalid input";
    getche();
    main();
    }
    getche();
    }

    void function_add_employee ()
    {
    EMPLOYEE employee;

    clrscr();
    cout<<"Employee Information"<<endl;
    cout<<"enter employee:";
    cin>>empl.employee;
    cout<<"enter address:";
    cin>>empl.address;
    cout<<"enter age:";
    cin>>empl.age;
    cout<<"enter employee's ID:";
    cin>>empl.emp_id;
    cout<<"payroll entry"<<endl;
    cout<<"enter rate:";
    cin>>pay.rate;
    cout<<"enter day:";
    cin>>pay.day;

    ArrayOfPeople [NumArrayOfPeople++] = employee;
    }



    void function_save ()
    {
    FILE* f = fopen ("D:oi.txt", "wb");
    if (f == NULL) return;

    fwrite (&NumArrayOfPeople, sizeof (int), 1, f);
    fwrite (ArrayOfPeople, sizeof (EMPLOYEE), NumArrayOfPeople, f);
    fclose (f);
    }

    void function_load ()
    {
    FILE* f = fopen ("D:oi.txt", "rb");
    if (f == NULL) return;

    fread (&NumArrayOfPeople, sizeof (int), 1, f);
    fread (ArrayOfPeople, sizeof (EMPLOYEE), NumArrayOfPeople, f);
    fclose (f);
    }

  • [color=Blue]You need to stop using main() as an entrance to next menu action. Use following pseudo-code:[/color]
    [code]
    int running=1;

    ...

    int main ()
    {
    function_load ();

    while (running)
    {
    print_menu ();
    cin >> choice;

    switch (choice)
    {
    case 1:
    function_add_employee ();
    break;

    case 2:
    function_show_info ();
    break;

    case 3:
    running = 0;
    break;
    }
    }

    function_save ();
    return 0;
    }
    [/code]
    [color=Blue]What show_info supposed to do? If you have 10 people in your file - which one do you show?[/color]
  • Correct me if I am making a horrible mistake, but why not write each record to the file after the structure has been filled in. This, I think, should avoid the limits set by the array. I have been doing it for some time with no working problems yet but then I could be just plain lucky.

    Regarding the question on editing a record you need to use the fseek() function. The parameters being....

    int fseek(FILE *stream, long offset,int origin);

    The first parameter is obviously your file pointer. The second is the record you wish to edit. Here it should be borne in mind that this parameter should be minus one from the record you want to edit. For example, if you wish to edit the ninth record then the parameter should read as 8*sizeof(your structure). This will position your pointer to the beginning of your 9th. record. The last parameter can be one of the following three...

    SEEK_CUR .... Current position of your pointer.
    SEEK_END .... End of File.
    SEEK_SET .... Beginning of file.

    Typically SEEK_SET is used to set the pointer for the record you wish to edit. Use the fread() function as usual setting the pointer to sizeof(your structure). The third parameter should,however, be set to 1. Edit the record and write back to the file using the same parameters as given for fread().

    To delete a record it is simply a matter of having an extra member in your structure which will show the status of the record. If you have to delete record 100 merely read that record back, edit the status field by setting it to D or whatever you prefer and writing it back. Now next when you browse the file just ensure that the file is read sequentially and when a D is encountered in the status field to move
    to the next record. You can update your database by reading all records whose status is not having a D and writing them to a temporary file. Once done delete the original file and rename the temporary file to the database name one. Hope this helps you out. Now try inserting a record within your database using the the same functions. It is not really difficult.

    But then I am just a toddler in C/C++.

  • I can't do this project on time...

    there's still something wrong on saving and showing information..

    #include
    #include
    #include
    #include


    typedef struct
    {
    char employee[15],address[33];
    int emp_id,age;
    }
    EMPLOYEE;

    EMPLOYEE ArrayOfPeople [32]; // 32 persons stored
    int NumArrayOfPeople;


    struct payroll{
    float basic,cola,gp,netpay;
    float sss,love,med,tax,totald;
    int rate,day;
    }pay;


    void print_menu ();
    void function_add_employee ();
    void function_save ();
    void function_show_info ();
    int running=1;


    int main ()
    {
    int choice;
    function_show_info ();

    while (running)
    {
    print_menu ();
    cin >> choice;

    switch (choice)
    {
    case 1:
    function_add_employee ();
    getche();
    break;

    case 2:
    function_show_info ();
    break;

    case 3:
    running = 0;
    break;
    }
    }

    function_save ();
    return 0;
    }





    void function_add_employee ()
    {
    EMPLOYEE employee;

    clrscr();
    cout<<"Employee Information"<<endl;
    cout<<"enter employee:";
    cin>>employee.employee;
    cout<<"enter address:";
    cin>>employee.address;
    cout<<"enter age:";
    cin>>employee.age;
    cout<<"enter employee's ID:";
    cin>>employee.emp_id;
    cout<<"payroll entry"<<endl;
    cout<<"enter rate:";
    cin>>pay.rate;
    cout<<"enter day:";
    cin>>pay.day;

    ArrayOfPeople [NumArrayOfPeople++] = employee;
    }



    void function_save ()
    {
    FILE* f = fopen ("D:oi.txt", "wb");
    if (f == NULL) return;

    fwrite (&NumArrayOfPeople, sizeof (int), 1, f);
    fwrite (ArrayOfPeople, sizeof (EMPLOYEE), NumArrayOfPeople, f);
    fclose (f);
    }

    void function_show_info ()
    {
    FILE* f = fopen ("D:oi.txt", "rb");
    if (f == NULL) return;

    cout<<"display Information:"<<endl;
    cout<<"employee name:"<<empl.employee<<endl;
    cout<<"employee address:"<<empl.address<<endl;
    cout<<"employee ID:"<<empl.emp_id<<endl;
    cout<<"employee age:"<<empl.age<<endl;
    cout<<"payslip information:"<<endl;
    cout<<"rate per day:"<<pay.rate<<endl;
    cout<<"number of days work:"<<pay.day<<endl;


    fread (&NumArrayOfPeople, sizeof (int), 1, f);
    fread (ArrayOfPeople, sizeof (EMPLOYEE), NumArrayOfPeople, f);
    fclose (f);
    }



    void print_menu ()
    {
    clrscr();
    char str[10];
    cout<<"M E N U"<<endl;
    cout<<"[1]add employee"<<endl;
    cout<<"[2]show information"<<endl;
    cout<<"[3]exit"<<endl;
    cout<<"E N T E R C H O I C E:";

    }

    thx alot...
  • [color=Blue]
    You did not answer my question of "what does show_info() supposed to do?"Also, show_info() and load() are separate actions, so why put them both into one function?
    [/color]
  • How could I have overlooked this. I think your file is not being created on the hard disk since you are using the following syntax for it...

    FILE* f = fopen ("D:oi.txt", "wb");

    There is only one whereas there should be two..like this...

    FILE* f = fopen ("D:\oi.txt", "wb");

    Secondly, why are you using the write mode in your file open. This simply deletes the existing file and all previous data is lost. I would suggest you use this ...

    FILE* f = fopen ("D:\oi.txt", "a");

    The reason why you use two slashes is simply this...

    C reads the first as an escape sequence and for the function to read a path definer we need to add a second . Hence \. To write the file use this...

    fwrite(&your_structure, sizeof(your_structure),1 , f);

    Reading back use fread() which has the same parameters as fwrite(). To read the entire file use fread() within a while statement as such..

    while(fread(&your_structure, sizeof(your_structure),1,f)==1)
    {

    Here you cout all the details you want to see on the screen.

    }

    There is a reason for the 1 after the equal sign. I leave it to you to find out.

    Hope this helps.

    But then I am a toddler in C/C++.

  • can you please edit my codes so i could finish it and pass it on time...it's really obvious that i'm having a hard time understanding this things and its because i'm still a begineer...i must admit programming is hard...

    please help me...i only got 3 more days to go...T_T..

    i just nid this options(add, save, edit, search by idnum, delete, view) for my program...

    please, i'm desperate now...T_T...

    actually im still in 1st year in this course so please understand my weakness...
  • [code]#include
    #include

    struct emp_info

    {
    char rec_status;
    char name[30];
    char address[30];
    int id, age;
    } employee;
    void menu(void);
    void add(void);
    void readfile(void);
    void getrecord(void);
    void edit(void);
    void delete(void);

    int main(void)

    {
    clrscr();
    menu();
    }

    void menu(void)
    {
    clrscr();
    int choice;
    cout<<"MENU"<<endl;
    cout<<"1. Add Employee"<<endl;
    cout<<"2. Read Complete File."<<endl
    cout<<"3.Get Specific Record<<endl;
    cout<<"4.Edit Record"<<endl;
    cout<<"5. Delete Record"<<endl<endl;
    cout<<"Enter your choice... ";
    cin>>choice;
    switch(choice)
    {
    case 1:
    add();
    break;
    case 2:
    readfile();
    break;
    case 3 :
    getrecord();
    break;
    case 4:
    edit();
    break;
    case 5:
    delete();
    break;
    }
    }

    void add(void)

    {
    char rpt;
    FILE *pf;
    pf=fopen("d:\emprecord.txt","a");
    do
    {
    cout<<"Enter employee name.. ";
    cin>>employee.name;
    cout<<endl;
    cout<<"Enter address.. ";
    cin<<employee.add;
    cout<<endl;
    cout<<"Enter employee id ... ";
    cin>>employee.id;
    cout<<endl;
    cout<<"Enter employee.age;
    cin>>employee.age;
    cout<<endl<<endl;
    fwrite(&employee, sizeof(employee),1,pf);
    cout<<"Do you wish to add another record? (y
    )";
    cin>>rpt;
    } while (rpt=='y' || rpt=='Y');
    fclose(pf);
    menu();
    }

    void readfile(void)
    {
    FILE *pf;
    pf=fopen("d:\emprecord.txt","r");
    while ( fread(&employee, sizeof(employee),1,pf)==1)
    {
    cout<<employee.name<<endl;
    cout<<employee.add<<endl;
    cout<<employee.id<<endl;
    cout<<employee.age<<endl<<endl<<endl;
    }
    fclose(pf);
    getch();
    menu();
    }[/code]

    That is your starter and should help you write the other functions as I described earlier. I have not had time to test the syntax nor build in any error capturing routines. That I leave to you.

    Bur then I am a toddler in C/C++.
  • A sidenote:

    sizeof(your_structure)

    This is very system- and compiler dependant. It will not work if you ever port your code to another compiler. The reason for this is "struct padding".
  • Thanks Lundin for pointing that out to me. What should I then do to make it compiler independent. So far it appears to have worked with VC6. Have yet to try it out with Visual Studio 2008 though. The clrscr() I have used since the original question included it. Looking forward for your answer.
  • You have to call fwrite for each member of the struct.

    fwrite(&employee.rec_status, sizeof(employee.rec_status),1,pf);
    fwrite(&employee.name, sizeof(employee.name),1,pf);

    and so on.


    (Note that it is safe to use sizeof() on the arrays just because they are statically allocated. If you had a pointer to the array it wouldn't work, since sizeof() would then give the size of the pointer itself.)
  • thank you so much for your help...
    i'm having my enthusiasm in programming again...

    btw can you pls show me an example of the fseek() function because its my first time encountering this code, our teacher did not discuss this one....
  • In continuation of my previous code here is a sample for the fseek() function. Do, however, keep in mind Lundin's explanation of the weakness while using the sizeof(your structure). I will be using it here since I have yet to work out the details and incorporating the same in my programs. I am sure Lundin will overlook this error on my part.

    And Lundin, you are absolutely right about this code failing if a pointer to the structure had been used. It was a major oversight on my part in not highlighting this point. My apologies to all those who may have read this thread and acted on it.

    Best of regards Lundin.

    [code]void getrecord(void)

    {
    clrscr();
    int location;
    FILE *pf;
    pf=fopen("d:\emprecord.txt", "r");
    cout<<"Enter record number to be read... ";
    cin>>location;
    location=location-1;
    fseek(pf, location*sizeof(employee), SEEK_SET);
    fread(&employee, sizeof(employee),1,pf);
    cout<<employee.name,<<endl;
    cout<<employee.add<<endl;
    cout<<employee.id<<endl;
    cout<<employee.age<<endl;
    getch();
    fclose(pf);
    menu();
    }([/code]

  • [code]#include
    #include
    #include
    #include


    struct emp_info
    {
    char rec_status;
    char name[30];
    char address[30];
    int id, age;
    }employee;

    void menu();
    void add();
    void readfile();
    void delfile();
    void getrecord();


    void main()
    {
    clrscr();
    menu();
    getche();
    }



    void menu()
    {
    clrscr();
    int choice;
    cout<<"MENU"<<endl;
    cout<<"1. Add Employee"<<endl;
    cout<<"2. Read Complete File."<<endl;
    cout<<"3. Delete all."<<endl;
    cout<<"4. getrecord"<<endl;
    cout<<"5. Exit"<<endl;
    cout<<"Enter your choice... ";
    cin>>choice;
    switch(choice)
    {
    case 1:
    clrscr();
    add();
    break;

    case 2:
    clrscr();
    readfile();
    break;

    case 3:
    clrscr();
    delfile();
    break;

    case 4:
    clrscr();
    getrecord();
    break;

    case 5:
    exit(0);
    }
    }

    void add()
    {

    char choice;
    FILE *pf;
    pf=fopen("D:\record.txt","a");
    if((pf=fopen("D:\record.txt", "a"))==NULL)
    {
    cout<<"could not open file";
    exit(1);
    }

    cout<<"Enter employee name.. ";
    gets(employee.name);
    cout<<endl;
    cout<<"Enter address.. ";
    gets(employee.address);
    cout<<endl;
    cout<<"Enter employee id ... ";
    cin>>employee.id;
    cout<<endl;
    cout<<"Enter employee age";
    cin>>employee.age;
    cout<<endl<<endl;
    fwrite(&employee, sizeof(employee),1,pf);
    fclose(pf);
    cout<<"Do you wish to add another record? (y\n)";
    cin>>choice;
    switch(choice)
    {
    case 'y':
    case 'Y':
    clrscr();
    add();
    break;
    default:
    menu();
    }
    }

    void readfile()
    {
    FILE *pf;
    pf=fopen("D:\record.txt","r");
    if((pf=fopen("D:\record.txt", "r"))==NULL)
    {
    cout<<"could not open file";
    }
    while (fread(&employee, sizeof(employee),1,pf)==1)
    {
    cout<<employee.name<<endl;
    cout<<employee.address<<endl;
    cout<<employee.id<<endl;
    cout<<employee.age<<endl<<endl<<endl;
    }
    fclose(pf);
    getche();
    menu();
    }


    void delfile()
    {
    clrscr();
    char choice;
    cout<<"Are you sure (y\n)??"<<endl;
    cin>>choice;
    switch(choice)
    {
    case 'y':
    case 'Y':

    remove("D:\record.txt");
    if(remove("D:\record.txt")==NULL)
    {
    cout<<"Oops, file not deleted";
    }
    else
    {
    cout<<"deleted";
    }
    getche();
    menu();
    break;
    default:
    menu();
    break;
    }
    }

    void getrecord()
    {
    clrscr();
    char choice;
    int location;
    FILE *pf;
    pf=fopen("D:\record.txt", "r");
    cout<<"Enter record number to be read... ";
    cin>>location1;
    location=location-1;
    fseek(pf, location*sizeof(employee), SEEK_SET);
    fread(&employee, sizeof(employee),1,pf);

    if(fread(&employee, sizeof(employee),1,pf)==NULL)
    {
    cout<<"Oops, file not found";
    }
    else
    {
    cout<<employee.name<<endl;
    cout<<employee.address<<endl;
    cout<<employee.id<<endl;
    cout<<employee.age<<endl;
    }
    getch();
    fclose(pf);
    cout<<"



    get record agen (y\n)??"<<endl;
    cin>>choice;
    switch(choice)
    {
    case 'y':
    case 'Y':

    getrecord();
    getche();
    break;
    default:
    menu();
    break;
    }
    }[/code]

    whenever i try to view the first employee by its record #1, the first employee is not displayed, the one that is being displayed is the second employee...

    how will i edit the employees data??

    honestly i don't know how..
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories