Monday 5 August 2013

intersection of data contained in 2 files

If set1.txt contain 5 6 7 9 4 10 3 2  and set2.txt contain 89 8 10 45 11 5
then Intersection.txt would contain 5  10


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cmath>
using namespace std;
class Intersection
{public:
    void  Read2Files(ifstream &input1 ,ifstream &input2, string store1[],string store2[] ,int &storecount1,int &storecount2 );
    void copy2fileintersection(string store1[],string store2[],int &storecount1,int &storecount2, ofstream &output);  //set has no repition of elements

};

int main()
{

int storecount1=0,storecount2=0,k;
string store1[50],store2[50];

    ifstream f1("set1.txt"),f2("set2.txt");
    ofstream f3("Intersection.txt");
    if(f3.fail())
    {
        cout<"The file to be written not accessed";
        exit(1);
    }
Intersection group;


 group.Read2Files(f1 ,f2,store1,store2,storecount1,storecount2 );

 group.copy2fileintersection(store1,store2,storecount1,storecount2,f3);




    return 0;
}

void Intersection::Read2Files(ifstream &input1 ,ifstream &input2, string store1[],string store2[] ,int &storecount1,int &storecount2 )
    {
        if(input1.fail()|| input2.fail())
        {
        cout<<"Files to be read accessing failed";
        exit(1);
         }

    int i,j;string element;
    for(i=0;!input1.eof();i++)
    {input1>>element;
        store1[i]=element;

    }
        for(j=0;!input2.eof();j++)
        {input2>>element;
        store2[j]=element;

        }
        storecount1=i-1; storecount2=j-1;

    }


    void Intersection::copy2fileintersection(string store1[],string store2[],int &storecount1,int &storecount2, ofstream &output)  //set has no repition of elements
    {
int i,j;
       for(i=0;i<storecount1;i++)
         {
           for(j=0;j<storecount2;j++)
           {
               if (store1[i]==store2[j])
                {cout<<store1[i]<<"  ";
                 output<<store1[i]<<"  ";
                 break;
                }

           }

         }
          cout<<"\nIntersection of elements copied to file";
    }

No comments:

Post a Comment