Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Write and read data to/from File (http://www.go4expert.com/forums/write-read-data-file-t15758/)

qqjay89 8Jan2009 12:07

Write and read data to/from File
 
:):)i 'm doing my assignment but i have some problem with this.can anyone help me to solve this problem .

here is my code:
Code:

#include<fstream.h>
#include<stdlib.h>
class variable
{
 protected:
  struct student
  {
  char name[100],course[100],id[10],gender;
  int  dob,mob,yob,age,ic;
  double cgpa,marks[10];
  }std[100];
  int i;
};
class student:public variable
{
 private:
 public:
  void reg()
  {
  i=1;
  std[i].dob=std[i].mob=std[i].yob=0;
  cout<<"Enter Student Name  : ";
  cin.ignore();
  cin.getline(std[i].name,100);
  cout<<"Enter Student IC    : ";
  cin.ignore();
  cin>>std[i].ic;
  cout<<"Enter Student Gender : ";
  cin.ignore();
  cin>>std[i].gender;
  cout<<"Enter Student Course : ";
  cin.ignore();
  cin.get(std[i].course,100);
  cout<<"Student Borthday    : "<<endl;;
  cout<<"Date  : ";
  cin.ignore();
  cin>>std[i].dob;
  cout<<"Month : ";
  cin.ignore();
  cin>>std[i].mob;
  cout<<"Year  : ";
  cin.ignore();
  cin>>std[i].yob;
  i++;
  }
  void dpl()
  {
  for(int a=1;a<=i;a++)
  {
    cout<<"Student Name    : "<<std[a].name<<endl;
    cout<<"Student ID      : "<<std[a].id<<endl;
    cout<<"Student IC      : "<<std[a].ic<<endl;
    cout<<"Student Gender  : "<<std[a].gender<<endl;
    cout<<"Student Course  : "<<std[a].course<<endl;
    cout<<"Student Birthday : "<<std[a].dob<<"/"<<std[a].mob<<"/"<<std[a].yob<<endl;
    cout<<"============================================="<<endl;
  }
  }
};
class exam:public student
{
};
class slt_menu:public exam
{
 private:
  int choice;
  char reply;
 public:
  void mainmenu()
  {
  cout<<"\t\t\t*******************************************"<<endl;
  cout<<"\t\t\t*          Extreme Technology            *"<<endl;
  cout<<"\t\t\t*                Collage                *"<<endl;
  cout<<"\t\t\t*          Student Detail System          *"<<endl;
  cout<<"\t\t\t*******************************************"<<endl;
  cout<<"\n\t\t\t1.Student Data System"<<endl;
  cout<<"\t\t\t2.Student Examination System"<<endl;
  cout<<"\t\t\t3.Exit From This Program"<<endl;
  cout<<"\n\t\t\tEnter Your Choice : ";
  do{
  cin>>choice;
    switch(choice)
    {
    case 1:
    {
      smenu();
      break;
    }
    case 2:
    {
      cout<<"****";
      break;
    }
    case 3:
    {
      exit(1);
      break;
    }
    default:
    {
      cout<<"\t\t\tYou Have Enter Invalid Input!!!Enter Again!!!"<<endl;
      cout<<"\t\t\tEnter Your Choice Again : ";
    }
    }
  }
  while(choice!=1 || choice !=2 ||choice !=3);
  }
  void smenu()
  {
  student st;
  cout<<"\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  cout<<"\t\t\t~              Student Data              ~"<<endl;
  cout<<"\t\t\t~                  System                ~"<<endl;       
  cout<<"\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  cout<<"\n\t\t\t1.Student Registration"<<endl;
  cout<<"\t\t\t2.Display Student Detail"<<endl;
  cout<<"\t\t\t3.Seacrh  Student Detail"<<endl;
  cout<<"\t\t\t4.Return To Main Menu"<<endl;
  cout<<"\n\t\t\tEnter YOur Choice : ";
  cin>>choice;
  do{
    switch(choice)
    {
    case 1:
    {
      do{
      reg();
      cout<<"Do You Wish To Add More Data??"<<endl;
      cin>>reply;
      }while(reply=='Y'||reply=='y');{}
      cout<<"Do You Want To Save & Return To Main Menu"<<endl;
      cin>>reply;
      if(reply=='Y'||reply=='y')
      {
      ofstream input("student_details.txt");
      input.write((char*)&st,sizeof(student));
      input.close();
      mainmenu();
      } 
      break;
    }
    case 2:
    {
      ifstream toread("student_details.txt");
      toread.read((char*)&st,sizeof(student));
      while(toread)
      {
      toread.read((char*)&st,sizeof(student));
      }
      toread.close();
      dpl();
    }
    case 3:
    {
     
    }
    case 4:
    {
      mainmenu();
    }
    default:
    {
      cout<<"\t\t\tYou Have Enter Invalid Input!!!Enter Again!!!"<<endl;
      cout<<"\t\t\tEnter Your Choice Again : ";
      cin>>choice;
    }
    }
  }while(choice!=1||choice!=2||choice!=3||choice!=4);
  }
};
void main()
{
 slt_menu m;
 m.mainmenu();
}

can anyone help me ???

sharmila 8Jan2009 16:59

Re: Write and read data to/from File
 
Can you let me know what is the problem that you got ..

qqjay89 8Jan2009 18:55

Re: Write and read data to/from File
 
i want to write a program that can let user input the data in the function call "reg" as show at below:
Code:

void reg()
  {
  i=1;
  std[i].dob=std[i].mob=std[i].yob=0;
  cout<<"Enter Student Name  : ";
  cin.ignore();
  cin.getline(std[i].name,100);
  cout<<"Enter Student IC    : ";
  cin.ignore();
  cin>>std[i].ic;
  cout<<"Enter Student Gender : ";
  cin.ignore();
  cin>>std[i].gender;
  cout<<"Enter Student Course : ";
  cin.ignore();
  cin.get(std[i].course,100);
  cout<<"Student Borthday    : "<<endl;;
  cout<<"Date  : ";
  cin.ignore();
  cin>>std[i].dob;
  cout<<"Month : ";
  cin.ignore();
  cin>>std[i].mob;
  cout<<"Year  : ";
  cin.ignore();
  cin>>std[i].yob;
  i++;
  }

so when the user have finish the input the data, it will automatic write to a file call "student_details.txt".
when the user want to display out the data that user input, the program can automatic read from the file "student_details.txt" and it will show the data in the screen with the function "dpl".
Code:

void dpl()
  {
  for(int a=1;a<=i;a++)
  {
    cout<<"Student Name    : "<<std[a].name<<endl;
    cout<<"Student ID      : "<<std[a].id<<endl;
    cout<<"Student IC      : "<<std[a].ic<<endl;
    cout<<"Student Gender  : "<<std[a].gender<<endl;
    cout<<"Student Course  : "<<std[a].course<<endl;
    cout<<"Student Birthday : "<<std[a].dob<<"/"<<std[a].mob<<"/"<<std[a].yob<<endl;
    cout<<"============================================="<<endl;
  }


sharmila 8Jan2009 19:11

Re: Write and read data to/from File
 
Is the problem is entering into the file or displaying from the file?

sharmila 8Jan2009 19:54

Re: Write and read data to/from File
 
In the code which you menctioned in 1st post as far as understand

1. in case1 of smenu() first you are asking the user " wish to add more data". if the user selected Y then again u r calling reg(), so that time all your previous data will be lost as the i value will be agian 1.. and it will try to store in std[1].. so only last record will be saved..
Better to initiliaze the value of 'i' in constructor.. or else instead of saving all the students valus at a time you can save one student details into the file and ask for another student details and save it at that time there is no need to take the array and you can save lot of memory..

2. To read instead of while( toread ) use while ( !toread.eof() ) so that you will know whether the file is reached end of file or not.. otherwise all the time it will be true and will continue forever.. and toread.read((char*)&st,sizeof(student)); is not correct.. as when you read from a file you will get a string.. it won't store in a structure.. so you won't get the correct o/p it seams.. and also here you are using st.. but in dpl() it is member variable std.. which contains last students records not the one which you are reading..


All times are GMT +5.5. The time now is 19:04.