Problem while file handling

B

bintom

I have the following File Handling program, wherein the modify method
is not modifying the concerned record, but is added the modified
record to the end of the file.

Also, the size of employee class is showing as 44, when it should be
37.

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>

struct date
{ int day, month, year; };

class employee
{ private: char flag, empcode[5], name[21];
date doj;
float sal;
public : void add()
{ char to_write;
flag=' ';
cout << "Enter empcode: "; cin >> empcode;
cin.ignore(1, '\n');
cout << "Enter name: "; cin.getline(name, 21);
cout << "Enter join year: "; cin >> doj.year;
cout << "Enter join month: "; cin >> doj.month;
cout << "Enter join day: "; cin >> doj.day;
cout << "Enter salary: "; cin >> sal;
cout << "\nWrite to disk? "; cin >> to_write;
if(toupper(to_write) == 'Y')
diskout();
}

void diskout()
{ ofstream outfile("Group.txt", ios::app);
outfile.write((char *)this, sizeof(*this));
}

void diskin()
{ ifstream infile("Group.txt");
cout << "CODE NAME Join Date
SALARY\n";
cout << "==== ==== =========
======\n\n";

while(infile.read((char *)this, sizeof(*this)))
output();

getch();
}

void output()
{ cout.setf(ios::left);
cout << setw(7) << empcode << setw(21) << name << " ";
(doj.day < 10)? cout << "0": cout << ""; cout << doj.day
<< "/";
(doj.month < 10)? cout << "0": cout << "" << "";
cout << doj.month << "/" << doj.year;
cout.unsetf(ios::left);
cout.setf(ios::showpoint);
cout << setw(10) << sal << "\n";
}

void modify()
{ char code[5], to_write; int count=0; long pos;

cout << "Enter employee code to be modified: ";
cin >> code;
fstream file("Group.txt", ios::ate | ios::in | ios::eek:ut);
file.seekg(0);
while(file.read((char *)this, sizeof(*this)))
{ if(strcmp(empcode, code) == 0)
{ output();
cout << "\nEnter empcode: "; cin >> empcode;
cin.ignore(1, '\n');
cout << "Enter name: "; cin.getline(name,
21);
cout << "Enter join year: "; cin >> doj.year;
cout << "Enter join month: "; cin >> doj.month;
cout << "Enter join day: "; cin >> doj.day;
cout << "Enter salary: "; cin >> sal;
cout << "\nWrite to disk? "; cin >> to_write;
pos = count*sizeof(employee);
file.seekp(pos, ios::beg);
cout << "\n\n" << pos << "\n\n";
getch();
if(toupper(to_write) == 'Y')
diskout();
break;
}
count++;
}
}
};

int main()
{ char choice;
employee e;

cout << "Sizeof employee = " << sizeof(employee);
getch();

do { system("Cls");
cout << "EMPLOYEE DATABASE MANAGEMENT\n\n";
cout << "1. Add a record\n";
cout << "2. Sort and List all records\n";
cout << "3. Modify a record\n";
cout << "0. Exit\n\n";
cout << "Enter your choice: ";
cin >> choice;
system("Cls");

switch(choice)
{ case '1': e.add(); break;
case '2': e.diskin(); break;
case '3': e.modify(); break;
}
} while(choice != '0');

return 0;
}
 
B

bintom

I have the following File Handling program, wherein the modify method
is not modifying the concerned record, but is added the modified
record to the end of the file.

Also, the size of employee class is showing as 44, when it should be
37.

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>

struct date
{ int day, month, year;  };

class employee
{ private: char flag, empcode[5], name[21];
           date doj;
           float sal;
  public : void add()
           { char to_write;
             flag=' ';
             cout << "Enter empcode: ";      cin >> empcode;
             cin.ignore(1, '\n');
             cout << "Enter name: ";         cin.getline(name, 21);
             cout << "Enter join year: ";    cin >> doj..year;
             cout << "Enter join month: ";   cin >> doj.month;
             cout << "Enter join day: ";     cin >> doj..day;
             cout << "Enter salary: ";       cin >> sal;
             cout << "\nWrite to disk? ";    cin >> to_write;
             if(toupper(to_write) == 'Y')
               diskout();
           }

           void diskout()
           { ofstream outfile("Group.txt", ios::app);
             outfile.write((char *)this, sizeof(*this));
           }

           void diskin()
           { ifstream infile("Group.txt");
             cout << "CODE             NAME          Join Date
SALARY\n";
             cout << "====             ====          =========
======\n\n";

             while(infile.read((char *)this, sizeof(*this)))
               output();

             getch();
           }

           void output()
           { cout.setf(ios::left);
             cout << setw(7) << empcode << setw(21) << name << "  ";
             (doj.day < 10)? cout << "0": cout << ""; cout << doj.day
<< "/";
             (doj.month < 10)? cout << "0": cout << "" << "";
             cout << doj.month << "/" << doj.year;
             cout.unsetf(ios::left);
             cout.setf(ios::showpoint);
             cout << setw(10) << sal << "\n";
           }

           void modify()
           { char code[5], to_write; int count=0; long pos;

             cout << "Enter employee code to be modified: ";
             cin >> code;
             fstream file("Group.txt", ios::ate | ios::in | ios::eek:ut);
             file.seekg(0);
             while(file.read((char *)this, sizeof(*this)))
             { if(strcmp(empcode, code) == 0)
               { output();
                 cout << "\nEnter empcode: ";      cin >> empcode;
                 cin.ignore(1, '\n');
                 cout << "Enter name: ";         cin.getline(name,
21);
                 cout << "Enter join year: ";    cin >> doj.year;
                 cout << "Enter join month: ";   cin >> doj.month;
                 cout << "Enter join day: ";     cin >> doj.day;
                 cout << "Enter salary: ";       cin >> sal;
                 cout << "\nWrite to disk? ";    cin >> to_write;
                 pos = count*sizeof(employee);
                 file.seekp(pos, ios::beg);
                 cout << "\n\n" << pos << "\n\n";
                 getch();
                 if(toupper(to_write) == 'Y')
                   diskout();
                 break;
               }
               count++;
             }
           }

};

int main()
{ char choice;
  employee e;

  cout << "Sizeof employee = " << sizeof(employee);
  getch();

  do { system("Cls");
       cout << "EMPLOYEE DATABASE MANAGEMENT\n\n";
       cout << "1. Add a record\n";
       cout << "2. Sort and List all records\n";
       cout << "3. Modify a record\n";
       cout << "0. Exit\n\n";
       cout << "Enter your choice: ";
       cin >> choice;
       system("Cls");

       switch(choice)
       { case '1': e.add();     break;
         case '2': e.diskin();  break;
         case '3': e.modify();  break;
       }
     } while(choice != '0');

   return 0;



}- Hide quoted text -

- Show quoted text -

A correction: I guess the size of employee should be 43, but it is
showing 44. Why?
 
R

red floyd

I have the following File Handling program, wherein the modify method
is not modifying the concerned record, but is added the modified
record to the end of the file.
Because you opened the outfile with ios::app.

Also, the size of employee class is showing as 44, when it should be
37.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>

You are clearly using a pre-Standard compiler here. You should upgrade
<fstream> and <iomanip> do said:
struct date
{ int day, month, year; };

class employee
{ private: char flag, empcode[5], name[21];
date doj;
float sal;
public : void add()
{ char to_write;
flag=' ';
cout<< "Enter empcode: "; cin>> empcode;
cin.ignore(1, '\n');
cout<< "Enter name: "; cin.getline(name, 21);
cout<< "Enter join year: "; cin>> doj.year;
cout<< "Enter join month: "; cin>> doj.month;
cout<< "Enter join day: "; cin>> doj.day;
cout<< "Enter salary: "; cin>> sal;
cout<< "\nWrite to disk? "; cin>> to_write;
if(toupper(to_write) == 'Y')
diskout();
}

void diskout()
{ ofstream outfile("Group.txt", ios::app);
outfile.write((char *)this, sizeof(*this));
}

Well, you open for append. That means that writes will be appended.
What did you expect?
void diskin()
{ ifstream infile("Group.txt");
cout<< "CODE NAME Join Date
SALARY\n";
cout<< "==== ==== =========
======\n\n";

while(infile.read((char *)this, sizeof(*this)))
output();

getch();
}

void output()
{ cout.setf(ios::left);
cout<< setw(7)<< empcode<< setw(21)<< name<< " ";
(doj.day< 10)? cout<< "0": cout<< ""; cout<< doj.day
<< "/";
(doj.month< 10)? cout<< "0": cout<< ""<< "";
cout<< doj.month<< "/"<< doj.year;
cout.unsetf(ios::left);
cout.setf(ios::showpoint);
cout<< setw(10)<< sal<< "\n";
}

void modify()
{ char code[5], to_write; int count=0; long pos;

cout<< "Enter employee code to be modified: ";
cin>> code;
fstream file("Group.txt", ios::ate | ios::in | ios::eek:ut);
file.seekg(0);
while(file.read((char *)this, sizeof(*this)))
{ if(strcmp(empcode, code) == 0)
{ output();
cout<< "\nEnter empcode: "; cin>> empcode;
cin.ignore(1, '\n');
cout<< "Enter name: "; cin.getline(name,
21);
cout<< "Enter join year: "; cin>> doj.year;
cout<< "Enter join month: "; cin>> doj.month;
cout<< "Enter join day: "; cin>> doj.day;
cout<< "Enter salary: "; cin>> sal;
cout<< "\nWrite to disk? "; cin>> to_write;
pos = count*sizeof(employee);
file.seekp(pos, ios::beg);
cout<< "\n\n"<< pos<< "\n\n";
getch();
if(toupper(to_write) == 'Y')
diskout();
break;
}
count++;
}
}
};
[redacted]
 
B

bintom

On 6/5/2010 6:03 AM, bintom wrote:> I have the following File Handling program, wherein the modify method
is not modifying the concerned record, but is added the modified
record to the end of the file.

Because you opened the outfile with ios::app.
Also, the size of employee class is showing as 44, when it should be
37.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>

You are clearly using a pre-Standard compiler here.  You should upgrade
your compiler to a modern one.  <iostream>, <fstream> and <iomanip> do
not have a ".h" appended.






struct date
{ int day, month, year;  };
class employee
{ private: char flag, empcode[5], name[21];
            date doj;
            float sal;
   public : void add()
            { char to_write;
              flag=' ';
              cout<<  "Enter empcode: ";      cin>>  empcode;
              cin.ignore(1, '\n');
              cout<<  "Enter name: ";         cin.getline(name, 21);
              cout<<  "Enter join year: ";    cin>>  doj.year;
              cout<<  "Enter join month: ";   cin>>  doj.month;
              cout<<  "Enter join day: ";     cin>>  doj.day;
              cout<<  "Enter salary: ";       cin>>  sal;
              cout<<  "\nWrite to disk? ";    cin>>  to_write;
              if(toupper(to_write) == 'Y')
                diskout();
            }
            void diskout()
            { ofstream outfile("Group.txt", ios::app);
              outfile.write((char *)this, sizeof(*this));
            }

Well, you open for append.   That means that writes will be appended.
What did you expect?




            void diskin()
            { ifstream infile("Group.txt");
              cout<<  "CODE             NAME          Join Date
SALARY\n";
              cout<<  "====             ====          =========
======\n\n";
              while(infile.read((char *)this, sizeof(*this)))
                output();
              getch();
            }
            void output()
            { cout.setf(ios::left);
              cout<<  setw(7)<<  empcode<<  setw(21)<<  name<<  "  ";
              (doj.day<  10)? cout<<  "0": cout<<  ""; cout<<  doj.day
<<  "/";
              (doj.month<  10)? cout<<  "0": cout<<  ""<<  "";
              cout<<  doj.month<<  "/"<<  doj.year;
              cout.unsetf(ios::left);
              cout.setf(ios::showpoint);
              cout<<  setw(10)<<  sal<<  "\n";
            }
            void modify()
            { char code[5], to_write; int count=0; long pos;
              cout<<  "Enter employee code to be modified: ";
              cin>>  code;
              fstream file("Group.txt", ios::ate | ios::in | ios::eek:ut);
              file.seekg(0);
              while(file.read((char *)this, sizeof(*this)))
              { if(strcmp(empcode, code) == 0)
                { output();
                  cout<<  "\nEnter empcode: ";      cin>>  empcode;
                  cin.ignore(1, '\n');
                  cout<<  "Enter name: ";         cin.getline(name,
21);
                  cout<<  "Enter join year: ";    cin>>  doj.year;
                  cout<<  "Enter join month: ";   cin>>  doj.month;
                  cout<<  "Enter join day: ";     cin>>  doj.day;
                  cout<<  "Enter salary: ";       cin>>  sal;
                  cout<<  "\nWrite to disk? ";    cin>>  to_write;
                  pos = count*sizeof(employee);
                  file.seekp(pos, ios::beg);
                  cout<<  "\n\n"<<  pos<<  "\n\n";
                  getch();
                  if(toupper(to_write) == 'Y')
                    diskout();
                  break;
                }
                count++;
              }
            }
};
[redacted]- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Rex,

Pls. note that the modify() method which is giving the problem is
opened in ios::ate mode and not
ios::app mode.

Bintom
 
J

Jonathan Lee

Rex,

Pls. note that the modify() method which is giving the problem is
opened in ios::ate mode and not
ios::app mode.

Bintom

Sure, but in modify() you are calling diskout() to do the actual
writing to the file. The diskout method reopens "Group.txt"
with ios::app, which causes the subsequent write to occur at
the end of the file.

Also, sizeof(employee) is going to be platform dependent.
It almost certainly is not the simple sum of the size of
the components because the compiler could pad data members.

Finally, I would really avoid doing this:

while(file.read((char *)this, sizeof(*this)))

As already mentioned, sizeof() is platform dependent. So
is the layout of the structure (to an extent). Who knows
how much you are reading and to where? Also "what". Your
structs in this program are using only built-ins, so this
is working, but if you had a pointer or a string this
would not work. Write proper serialize and unserialize
methods so that the conversion from disk to memory is
well defined.

--Jonathan
 
B

bintom

Sure, but in modify() you are calling diskout() to do the actual
writing to the file. The diskout method reopens "Group.txt"
with ios::app, which causes the subsequent write to occur at
the end of the file.

Also, sizeof(employee) is going to be platform dependent.
It almost certainly is not the simple sum of the size of
the components because the compiler could pad data members.

Finally, I would really avoid doing this:

   while(file.read((char *)this, sizeof(*this)))

As already mentioned, sizeof() is platform dependent. So
is the layout of the structure (to an extent). Who knows
how much you are reading and to where? Also "what". Your
structs in this program are using only built-ins, so this
is working, but if you had a pointer or a string this
would not work. Write proper serialize and unserialize
methods so that the conversion from disk to memory is
well defined.

--Jonathan

Hey Jonathan,

Thanks for pointing me to the mistake. The program is working fine
now.

Where can I find some info on serialised and unserialized methods in C+
+ and what it can do for me?

Thanks again,
Bintom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top