c++ - Comparing two data files -


i have 2 data files, in there data points same values, need produce , output in i'll have common data points.

here's code:

#include<iostream> #include<cmath> #include<fstream>  using namespace std;  int main()  {   long double p_1,pd_1, age, mag, p_2, pd_2, dm_2, tsc_2, s_2, d_2, lum_2;    double data1[1659]; double data2[1688];  std::ifstream fin ("sort.txt",std::ifstream::in); std::ifstream gin ("sort1.txt", std::ifstream::in);  for(int i=0; i<1659; i++) {     fin>> p_1 >> pd_1 >> age >> mag;     data1[i]= p_1; } for(int i=0; i<1688; i++) {     gin>> p_2 >> pd_2 >> dm_2 >> tsc_2 >> s_2 >> d_2 >> lum_2;     data2[i]= p_2; }  for(int i=0; i<1659; i++) {     if(data1[i]==data2[i])         cout<<p_2<<"\t"<<pd_2<<"\t"<<dm_2<<"\t"<<tsc_2<<"\t"<<s_2<<"\t"<<d_2<<"\t"<<lum_2<<endl; }  return(0); 

}

i did not produce , output file wanted see output looks like. please me out here.

if data ordered, should work.


this code poor, use draft. (magic numbers, etc.)

#include <iostream> #include <cmath> #include <fstream>  using namespace std;  struct rec1 {     long double p_1, pd_1, age, mag; };  struct rec2 {     long double p_2, pd_2, dm_2, tsc_2, s_2, d_2, lum_2; };  int main() {     rec1 data1[1659];     rec2 data2[1688];      std::ifstream fin("sort.txt", std::ifstream::in);     std::ifstream gin("sort1.txt", std::ifstream::in);      (int = 0; i<1659; i++)     {         fin >> data1[i].p_1 >> data1[i].pd_1 >> data1[i].age >> data1[i].mag;     }      (int j = 0; j<1688; j++)     {         gin >> data2[j].p_2 >> data2[j].pd_2 >> data2[j].dm_2 >> data2[j].tsc_2 >> data2[j].s_2 >> data2[j].d_2 >> data2[j].lum_2;     }      int = 0, j = 0;     while (i < 1659 && j < 1688)     {         if (data1[i].p_1 < data2[j].p_2) i++;         else if (data1[i].p_1 > data2[j].p_2) j++;         else {             cout << data2[j].p_2 << "\t" << data2[j].pd_2 << "\t" << data2[j].dm_2 << "\t" << data2[j].tsc_2 << "\t" << data2[j].s_2 << "\t" << data2[j].d_2 << "\t" << data2[j].lum_2 << endl;             i++; j++;         }     }      fin.close();     gin.close();      return 0; } 

note not necessary store whole dataset in memory. can intersection "on fly."


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -