Database::problem printing last data twice.

This is a discussion on Database::problem printing last data twice. within the C++ Programming forums, part of the General Programming Boards category; I have made a database kind of stuff. Problem I am facing is when i try to print whole database(list_employee ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Database::problem printing last data twice.

    I have made a database kind of stuff. Problem I am facing is when i try to print whole database(list_employee function marked in colour),last record is printed twice.If I have opened new file without adding anything,then I get some garbage result.I have spend around 5 to 6 hour but could not find it.Please help.



    Code:
    class employee
    {
    private:
      fstream empfile;
      char name[40];
      int age;
      float bs;
      void add_to_file();
      void search_from_file(char* emp_name);
    public:
      employee(char *filename);
      void add_employee();
      void edit_employee();
      void del_employee();
      void list_employee();
      ~employee(){empfile.close();}
    };
    
    employee::employee(char *filename)
    {
      empfile.open(filename,ios::binary|ios::ate|ios::in|ios::out);
      if(!empfile)
      {
        cout<<"Error opening file\nPress any key to exit...";
        getch();
        exit(0);
      }
    }
    
    void employee::add_to_file()
    {
    empfile.write((char*)&this->name,sizeof(employee)-sizeof(ifstream));
    empfile.clear();
    }
    
    void employee::add_employee()
    {
    cout<<"name..."; cin.getline(name,40);
    cout<<"Age..."; cin>>age;
    cout<<"Basic Salary..."; cin>>bs;
    empfile.seekp(0,ios::end);
    add_to_file();
    }
    
    void employee::list_employee()
    {
      empfile.seekg(0,ios::beg);
    while(empfile)
    {
    empfile.read((char*)(&this->name),sizeof(employee)-sizeof(ifstream));
      cout<<"Name..."<<name<<endl;
      cout<<"Age..."<<age<<endl;
      cout<<"Basic Salary..."<<bs<<"\n\n";
    }
    empfile.clear();
    }
    
    int main()
    {
    employee niec("c:\\windows\\desktop\\employee.dat");
    int opt;
    while(1)
    {
    clrscr();
    cout<<"1.Add employee\n2.Edit Employee\n3.List Employee\n4.Exit";
    cout<<"\nEnter your option...";
    cin>>opt;
    cin.get();
      switch(opt)
      {
        case 1:niec.add_employee();break;
    //    case 2:niec.edit_employee();break;
        case 3:niec.list_employee();break;
        case 4:niec.employee::~employee();
    	   exit(0);
    	   break;
      }
    cout<<"Press any key to continue....";
    getch();
    }
    getch();
    return(0);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    The problem is in your while(empfile) loop in employee::list_employee(). When you use the stream as the condition to your while loop, it only breaks the loop when an error occurs. So when reading in the file, after you have read in the last record, no error has occurred and the while loop continues. Then you try to read again and the read fails without filling the data with new information. The information from the previous read is still in the variables, and so that gets printed out again.

    The solution is to check the result of the read function. You can do that by placing it directly into the while loop, or you can put the call to read in an if statement and break the loop if the read is not successful.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    thanks,it now works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  4. Dynamic data members?
    By confusalot in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 10:15 AM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM

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