Docstoc

C++ program merging two files

Document Sample
C++ program merging two files Powered By Docstoc
					                                        29
// This program merging two files
#include<fstream.h>
#include<conio.h>
struct employee
{
        char name[20];
        int empno;
        char status[12];
        float payrate;
};
employee emp;
main()
{
        int n,i,size,n1;
        ofstream ofile;         //creating first file
        ofile.open("OLD.DAT");
        cout<< "\n Enter the number of employee in first file";
        cin>>n;
        size = sizeof(employee);
        for (i=0;i<n;i++)
        {
                cout<<"\n Enter Name ";
                cin>>emp.name;
                cout<<"\n Enter emp no ";
                cin>>emp.empno;
                cout<<"\n Enter the status ";
                cin>>emp.status;
                cout<<"\n Enter the payrate ";
                cin>>emp.payrate;
                ofile.write((char *)& emp,size);
        }
        ofstream tfile;
        tfile.open("OLD1.DAT");            // creating second file
        cout<<"\n Enter the number of employee in second file ";
        cin>>n1;
        size = sizeof(employee);
        for (i=0;i<n1;i++)
        {
                cout<<"\n Enter Name ";
                cin>>emp.name;
                cout<<"\n Enter the empno ";
                cin>>emp.empno;
                cout<<"\n Enter the status ";
                cin>>emp.status;
                cout<<"\n Enter the payrate ";
                cin>>emp.payrate;
                tfile.write((char *)& emp,size);
        }
        ofile.close();
        tfile.close();
        ifstream afile, bfile;    //Merging two files into the third file
        afile.open("OLD.DAT");
        bfile.open("OLD1.DAT");
        ofstream mfile;
        mfile.open("NEW.DAT",ios ::app);
        while(afile)
        {
                afile.read((char *)&emp , size);
                mfile.write((char *)&emp , size);
        }
        while(bfile)
        {
                bfile.read((char *)&emp , size);
                mfile.write((char *)&emp , size);
        }
        ofile.close();
        tfile.close();
                                       Page 1
                                       29
       mfile.close();
       return 0;
}
        step by step explanation:
1.the header file"conio.h"is used to define the function like getch and clrscr.
2.struct employee is a structure type function.
3. int empno is a int type function.
4.main is also a void type function.
5.for comes under the category of loops it is used to repeating the steps.
6.while is a conditional statement which is used to check the condition.
7. return 0 is used when main function is not defined in the begining.




                                     Page 2

				
DOCUMENT INFO
Shared By:
Stats:
views:152
posted:3/15/2012
language:English
pages:2
Description: C programs on various topics such as count and display number of vowels used in a text file, function to append records, function to calculate average word size, C function along with program to merge two files using file handling concept of C programming language.