Help me to implement file concept in this problem , i want to save the structure data to a file and Display from file.Please help me.

#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
#define max 20
struct employee
{
 char name[20];
 long int code;
 char designation[20];
 int exp;
 int age;
};
int num;
employee emp[max],tempemp[max],sortemp[max],sortemp1[max];
void main()
{
 clrscr();
 void build();
 void list();
 void insert();
 void deletes();
 void edit();
 void search();
 void sort();
 char option;
 void menu();
 menu();
 while((option=cin.get())!='q')
 {
  switch(option)
  {
   case 'b':
		     build();
		     break;
   case 'l':
		     list();
		     break;
   case 'i':
		     insert();
		     break;
   case 'd':
		     deletes();
		     break;
   case 'e':
		   edit();
		    break;
   case 's':
		    search();
		    break;
   case 'n':
		     sort();
		     break;
  }
   menu();
  }
 }
 void menu()
 {
  clrscr();
  //highvideo();
cout<<"		      ";
cprintf("*****WelCome To Employee Data Centre*****");
normvideo();
cout<<endl;
cout<<"		         ";
cout<<"Press  b---->Built The Employee Table\n";
cout<<"		         ";
cout<<"Press  l---->List The Employee Table\n";
cout<<"		         ";
cout<<"Press  i---->Insert New Entry\n";
cout<<"		         ";
cout<<"Press  d---->Delete An Entry\n";
cout<<"		         ";
cout<<"Press  e---->Edit An Entry\n";
cout<<"		         ";
cout<<"Press  s---->Search a record\n";
cout<<"		         ";
cout<<"Press  n---->Sort The Table\n";
cout<<"		         ";
cout<<"Press  q---------->QUIT\n";
cout<<"		         ";
cout<<"Option Please ----->";
}

void build()
{

 clrscr();
 highvideo();
 cprintf("Build The Table");
 cout<<endl;
 normvideo();
 cout<<"maximum number of entries  -----  >  20"<<endl;
 cout<<"how many do you want    ----->";
 cin>>num;
 cout<<"Enter The Following Items:\n";
 for(int i=0;i<=num-1;i++)
 {
  cout<<" Name:\t	";
  cin>>emp[i].name;
  cout<<"Code:\t	";
  cin>>emp[i].code;
  cout<<"Designation:\t	";
  cin>>emp[i].designation;
  cout<<"Years of Experience:\t	";
  cin>>emp[i].exp;
  cout<<"Age:\t	";
  cin>>emp[i].age;
  cout<<"\n\n";
 }
  cout<<"going to main menu";
  delay(500);
}

void  list()
{
 clrscr();
 highvideo();
 cprintf(" ********List The Table********\n");
 cout<<endl;
 normvideo();
 cout<<" Name	    Code      Designation       Years(EXP)	   Age\n";
 cout<<"\t------------------------------------------------------\n";
 for(int i=0;i<=num-1;i++)
 {
  cout<<setw(13)<<emp[i].name;
  cout<<setw(6)<<emp[i].code;
  cout<<setw(15)<<emp[i].designation;
  cout<<setw(10)<<emp[i].exp;
  cout<<setw(15)<<emp[i].age;
  cout<<endl;
 }
  cout<<"going to main menu";
 getch();
  }
  void insert()
  {
  clrscr();
  int i=num;
  num+=1;
  highvideo();
  cprintf("Insert New Record:*\n");
  cout<<endl;
  normvideo();
  cout<<"Enter The Following Items*\n";
  cout<<"Name \n   ";
  cin>>emp[i].name;
  cout<<"Code \n	";
  cin>>emp[i].code;
  cout<<"Designation \n	";
  cin>>emp[i].designation;
  cout<<"Years of Experience \n	";
  cin>>emp[i].exp;
  cout<<"Age	";
  cin>>emp[i].age;
  cout<<endl<<endl;
  cout<<"going to main menu\n";
  delay(500);

  }


  void deletes()
  {
   clrscr();
   highvideo();
   int code;
   int check;
   cprintf("Delete An Entry\n");
   normvideo();
   cout<<endl;
   cout<<"Enter An JobCode To Delete That Entry	";
   cin>>code;
   int i;
   for(i=0;i<=num-1;i++)
   {
    if(emp[i].code==code)
    {
      check=i;
    }
   }
   for(i=0;i<=num-1;i++)
   {
    if(i==check)
    {
    continue;
    }
    else
    {
    if(i>check)
    {
     tempemp[i-1]=emp[i];
    }
    else
    {
     tempemp[i]=emp[i];
    }
     }
   }
  num--;

  for(i=0;i<=num-1;i++)
  {
   emp[i]=tempemp[i];
  }
 }

void edit()
{
 clrscr();
 int jobcode;
 highvideo();
 cprintf("          Edit An Entry           ");
 cout<<endl;
 cout<<endl;
 int i;
 void editmenu();
 void editname(int);
 void editcode(int);
 void editdes(int);
 void editexp(int);
 void editage(int);
 char option;
 normvideo();
 cout<<"Enter An job code To Edit An Entry---- \n ";
 cin>>jobcode;
  editmenu();
 for(i=0;i<=num-1;i++)
   {
    if(emp[i].code==jobcode)
    {

while((option=cin.get())!='q')
{
      switch(option)
      {
       case 'n':
				   editname(i);
				   break;
       case 'c':
				   editcode(i);
				   break;
       case 'd':
				   editdes(i);
				   break;
       case 'e':
				   editexp(i);
				   break;
       case 'a':
				  editage(i);
				  break;
     }
   editmenu();
    }
  }
  }
  }
  void editmenu()
  {
   clrscr();
   cout<<"		       What Do You Want To edit\n";
   cout<<"		       n--------->Name\n";
   cout<<"		       c--------->Code\n";
   cout<<"		       d--------->Designation\n";
   cout<<"		       e--------->Experience\n";
   cout<<"		       a--------->Age\n";
   cout<<"                     q----->QUIT\n";
   cout<<"                    Options Please ---->>>  ";
  }
  void editname(int i)
  {
     cout<<"Enter New Name----->  \n";
     cin>>emp[i].name;
  }
  void editcode(int i)
  {
   cout<<"Enter New Job Code----->   \n";
   cin>>emp[i].code;
  }
  void editdes(int i)
  {
   cout<<"enter new designation----->  \n";
   cin>>emp[i].designation;
  }
  void editexp(int i)
  {
   cout<<"Enter new Years of Experience\n";
   cin>>emp[i].exp;
  }
  void editage(int i)
  {
   cout<<"Enter new Age";
   cin>>emp[i].age;
  }

void search()
{
 clrscr();
  highvideo();
  cprintf("Welcome To Search Of Employee Database");
  normvideo();
  cout<<endl;
  cout<<endl;
  int jobcode;
  cout<<"You Can Search Only By Jobcode Of An Employee\n";
  cout<<"Enter Code Of An Employee";
 cin>>jobcode;
 for(int i=0;i<=num-1;i++)
   {
    if(emp[i].code==jobcode)
    {

    cout<<" Name       Code     Designation     Years(EXP)	  Age\n";
 cout<<"     ------------------------------------------------------\n";
  cout<<setw(13)<<emp[i].name;
  cout<<setw(6)<<emp[i].code;
  cout<<setw(15)<<emp[i].designation;
  cout<<setw(10)<<emp[i].exp;
  cout<<setw(15)<<emp[i].age;
  cout<<endl;
 }

  }
    cout<<"going to main menu";
 getch();


}

void sort()
{
 clrscr();
 highvideo();
 cprintf("Sort The Databse By JobCode");
 normvideo();
 void sortmenu();
 void sortname();
 void sortcode();
 void sortdes();
 void sortexp();
 char option;
 void sortage();

 cout<<endl;
 cout<<endl;
 sortmenu();
 while((option=cin.get())!='q')
 {
  switch(option)
  {
   case 'n':
				 sortname();
				 break;
   case 'c':
				 sortcode();
				 break;
   case 'd':
				 sortdes();
				 break;
   case 'e':
				 sortexp();
				 break;
   case 'a':
				 sortage();
				 break;
   }
   sortmenu();
  }
 }


 void sortmenu()
 {
    clrscr();
   cout<<"	 What Do You Want To edit\n";
   cout<<"	 n--------->Name\n";
   cout<<"       c--------->Code\n";
   cout<<"	 d--------->Designation\n";
   cout<<"	 e--------->Experience\n";
   cout<<"	 a--------->Age\n";
   cout<<"       q----->QUIT\n";
   cout<<"   Options Please ---->>>	";
  }



void sortname()
{
 clrscr();
 int i,j;
 struct employee temp[max];
 for(i=0;i<=num-1;i++)
 {
  sortemp1[i]=emp[i];
 }
 for(i=0;i<=num-1;i++)
  {
   for(j=0;j<=num-1;j++)
   {
    if(strcmp(sortemp1[i].name,sortemp1[j].name)<=0)
    {
     temp[i]=sortemp1[i];
     sortemp1[i]=sortemp1[j];
     sortemp1[j]=temp[i];
    }
   }
 }

 for( i=0;i<=num-1;i++)
   {

    cout<<"  Name      Code	 Designation	 Years(EXP)	 Age\n";
 cout<<"     ------------------------------------------------------\n";
 for( i=0;i<=num-1;i++)
 {
  cout<<setw(13)<<sortemp1[i].name;
  cout<<setw(6)<<sortemp1[i].code;
  cout<<setw(15)<<sortemp1[i].designation;
  cout<<setw(10)<<sortemp1[i].exp;
  cout<<setw(15)<<sortemp1[i].age;
  cout<<endl;
 }
  cout<<"Press Any Key To Go Back";
 getch();

} }

void sortcode()
{
 clrscr();
 int i,j;
 struct employee temp[max];
 for(i=0;i<=num-1;i++)
 {
  sortemp1[i]=emp[i];
 }
 for(i=0;i<=num-1;i++)
  {
   for(j=0;j<=num-1;j++)
   {
    if(sortemp1[i].code<sortemp1[j].code)
    {
     temp[i]=sortemp1[i];
     sortemp1[i]=sortemp1[j];
     sortemp1[j]=temp[i];
    }
   }
 }

 for( i=0;i<=num-1;i++)
   {

    cout<<"  Name       Code	  Designation	  Years(EXP)	 Age\n";
 cout<<"     ------------------------------------------------------\n";
 for( i=0;i<=num-1;i++)
 {
  cout<<setw(13)<<sortemp1[i].name;
  cout<<setw(6)<<sortemp1[i].code;
  cout<<setw(15)<<sortemp1[i].designation;
  cout<<setw(10)<<sortemp1[i].exp;
  cout<<setw(15)<<sortemp1[i].age;
  cout<<endl;
 }
  cout<<"Press Any Key To Go Back";
 getch();

} }


void sortdes()
{
 clrscr();
 int i,j;
 struct employee temp[max];
 for(i=0;i<=num-1;i++)
 {
  sortemp1[i]=emp[i];
 }
 for(i=0;i<=num-1;i++)
  {
   for(j=0;j<=num-1;j++)
   {
    if(strcmp(sortemp1[i].designation,sortemp1[j].designation)<=0)
    {
     temp[i]=sortemp1[i];
     sortemp1[i]=sortemp1[j];
     sortemp1[j]=temp[i];
    }
   }
 }

 for( i=0;i<=num-1;i++)
   {

    cout<<"  Name      Code    Designation      Years(EXP)	  Age\n";
 cout<<"     ------------------------------------------------------\n";
 for( i=0;i<=num-1;i++)
 {
  cout<<setw(13)<<sortemp1[i].name;
  cout<<setw(6)<<sortemp1[i].code;
  cout<<setw(15)<<sortemp1[i].designation;
  cout<<setw(10)<<sortemp1[i].exp;
  cout<<setw(15)<<sortemp1[i].age;
  cout<<endl;
 }
  cout<<"Press Any Key To Go Back";
 getch();

} }

void sortage()
{
 clrscr();
 int i,j;
 struct employee temp[max];
 for(i=0;i<=num-1;i++)
 {
  sortemp1[i]=emp[i];
 }
 for(i=0;i<=num-1;i++)
  {
   for(j=0;j<=num-1;j++)
   {
    if(sortemp1[i].age<sortemp1[j].age)
    {
     temp[i]=sortemp1[i];
     sortemp1[i]=sortemp1[j];
     sortemp1[j]=temp[i];
    }
   }
 }

 for( i=0;i<=num-1;i++)
   {

    cout<<" Name     Code       Designation      Years(EXP)	 Age\n";
 cout<<"     ------------------------------------------------------\n";
 for( i=0;i<=num-1;i++)
 {
  cout<<setw(13)<<sortemp1[i].name;
  cout<<setw(6)<<sortemp1[i].code;
  cout<<setw(15)<<sortemp1[i].designation;
  cout<<setw(10)<<sortemp1[i].exp;
  cout<<setw(15)<<sortemp1[i].age;
  cout<<endl;
 }
  cout<<"Press Any Key To Go Back";
 getch();

} }


void sortexp()
{
 clrscr();
 int i,j;
 struct employee temp[max];
 for(i=0;i<=num-1;i++)
 {
  sortemp1[i]=emp[i];
 }
 for(i=0;i<=num-1;i++)
  {
   for(j=0;j<=num-1;j++)
   {
    if(sortemp1[i].exp<sortemp1[j].exp)
    {
     temp[i]=sortemp1[i];
     sortemp1[i]=sortemp1[j];
     sortemp1[j]=temp[i];
    }
   }
 }

 for( i=0;i<=num-1;i++)
   {

    cout<<" Name      Code     Designation     Years(EXP)	  Age\n";
 cout<<"     ------------------------------------------------------\n";
 for( i=0;i<=num-1;i++)
 {
  cout<<setw(13)<<sortemp1[i].name;
  cout<<setw(6)<<sortemp1[i].code;
  cout<<setw(15)<<sortemp1[i].designation;
  cout<<setw(10)<<sortemp1[i].exp;
  cout<<setw(15)<<sortemp1[i].age;
  cout<<endl;
 }
  cout<<"Press Any Key To Go Back";
 getch();

} }

I want to store these data to a file and retrieve it and display when i execute the ' list function ' ( when i choose 'l') . Please help me .

Fire up google and learn the basic FILESTREAMS. After doing that, the rest will be very simple to understand.

If you have any prob after that. Then you can post your question up.

when i tried to pass my structure variable , in function it is saying undefined variable.

narrow it down for us by posting only what you tried to do but failed. I don't have the time to read that entire program to figure out what you are talking about.

First define how you want the file to be structured. Given the variable length of several of the member variables (strings) within struct objects I would probably go with writing each variable to file separately rather than trying to use the write() stream member function. Given the possibility of space within the string variables I would probably make it a comma (or some other default variable) delimited file. I would probably write all variable members of a given ogject to the same "line".

Once the file structure has been laid out then you can use the stream methods you are familiar with using cout to write each member variable, delimiters, etc to the file. The only new concept is how to use a file stream instead of an ostream to do it. Rather than writing all the code over and over again any time you want to write to the file, it is a common practice to place the appropriate code up in a member function or overload the << operator so you can call the member function or the << operator with the appropriate stream to use when you want to write an object to file.

This article has been dead for over six months. Start a new discussion instead.