Thread: merging two files

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    25

    merging two files

    i made a program in dev c++ to merge contents of two files into a single file but the merged file is showing garbage outputs.

    question:-
    Write a C++ program to merge the content of two files namely “Employee.txt”
    and
    “Salary.txt” into “EmployeeDetails.txt”.

    Sample contents of Employee.txt
    EmployeeName EmployeeNumber Department Age
    Rohit 12134 CSE 30
    Ajay 13543 ECE 32
    Bikash 12345 EEE 40

    Sample contents of Employee.txt
    GrossSalary NetSalary
    12222 10000
    22222 20000
    32222 30000
    Sample contents of the merged file EmployeeDetails.txt

    EmployeeName EmployeeNumber Department Age GrossSalary Netsal
    Rohit 12134 CSE 30 12222 10000
    Ajay 13543 ECE 32 22222 20000
    Bikash 12345 EEE 40 32222 30000

    Code:
    #include<iostream>
    #include<conio.h>
    #include<fstream>
    using namespace std;
    
    class emp
    {
          int num,age;
          char name[20],dep[5];
          
          public:
              void getdata()
              {
                 cout<<"\n\n  Name   = ";
                 cin>>name;
                 cout<<"\n Emp Num   = ";
                 cin>>num;
                 cout<<"\n Department= ";
                 cin>>dep;
                 cout<<"\n Age       = ";
                 cin>>age;
              }
              void display1()
              {  
                cout<<"\n"<<name<<"\t"<<num<<"\t"<<dep<<"\t\t"<<age;
              }  
              
    };
    
    class sal
    {
          float gs,ns;
           public:
               void getsal()
               {
                 cout<<"\n Gross sal = ";
                 cin>>gs;
                 cout<<"\n Net sal   = ";
                 cin>>ns;
               }
               void display2()
               {
                 cout<<"\t"<<gs<<"\t"<<ns;
               }          
    };
    
    void display()
    {
       emp e;sal s;
       fstream fil1;
       
       fil1.open("empdetails.txt",ios::in|ios::out);
    
       cout<<"\n\n Name \t Emp Num \t Dep \t Age \t Gross Sal \t Net Sal \n";  
    
       do
       {
        fil1.read((char*)&e,sizeof(e));
        e.display1();
        
        fil1.read((char*)&s,sizeof(s));
        s.display2();
       }while(fil1);
       
    }
                 
    int main()
    {
        int n;
        emp e1;sal s1;
        fstream fil1,fil2,fil3;
        
        fil1.open("emp.txt",ios::in|ios::out);
        fil2.open("sal.txt",ios::in|ios::out);
        fil3.open("empdetails.txt",ios::in|ios::out);
       
        cout<<"\n How many employee details do you want to enter = ";
        cin>>n;
        
        cout<<"\n Enter the deatils one by one \n";
        for(int i=0;i<n;i++)
        {
            e1.getdata();
            fil1.write((char*)&e1,sizeof(e1));
            
            s1.getsal();
            fil2.write((char*)&s1,sizeof(s1));
            
            fil3.write((char*)&e1,sizeof(e1));//in these 2 steps i try to 
            fil3.write((char*)&s1,sizeof(s1));//merge the contents of  
                                                       //   both objects
        }
        fil1.close();
        fil2.close();
        fil3.close();
        
        cout<<"\n\n\t\t Merged file contents \n\n\t\t";
        display();
        getch();
        return 0;
    }
    i am new to file programming.so please help me . i had my oops lab exam next week.thank u.
    Last edited by shikhardeep; 11-12-2011 at 04:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merging files (*.001, *.002 etc.)
    By Altertwin in forum Windows Programming
    Replies: 4
    Last Post: 07-11-2010, 01:24 PM
  2. merging files into one
    By mbooka in forum C Programming
    Replies: 6
    Last Post: 02-16-2006, 07:31 PM
  3. Merging files....
    By girlzone in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2003, 02:39 PM
  4. Merging Files
    By TankCDR in forum C Programming
    Replies: 0
    Last Post: 10-27-2001, 06:48 PM
  5. Merging Files
    By Natase in forum C Programming
    Replies: 6
    Last Post: 09-25-2001, 07:10 PM