[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

files comparing questions

Hi experts,
I have the 2 files as followings:

oldfile.csv
111,222,333
111a,222a,333a
111b,222b,333b

newfile.csv
111,223,333
111a,2233,333a
111b,222b,333b
111c,222c,333c

I want to compare these 2 files by 3 fields and
1) if all 3 fields are same, then ignore it
2) if one of the fields changed, then put "Y" in the front of newfn3, and write to diff.csv file
3) if new fields on newfile.csv, then write to diff.csv file
So the output file is going to be like this:

diff.csv
111,223,Y333
111a,2233,Y333a
111c,222c,333c

Here is my comparing program, and
Any experts can help me put "Y" in the front of newfn3 ?

#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <set>
using namespace std;

///////////////////////////////////
// this function can get any fields
////////////////////////////////////
string GetField(string &aStr, int aFieldNum, char aDelim)
{
    istringstream ss(aStr);
    string field;
    while (getline(ss, field, ',') && aFieldNum > 0 )
    {
        --aFieldNum;
    }
    return field;
};

int main(int argc, char *argv[])
{
            ifstream oldfin;
            ifstream newfin;
            ofstream fout;
            oldfin.open("oldfile.csv");
            newfin.open("newfile.csv");
            fout.open("diff.csv");

            string lineOld;
            string lineNew;
            
            set<string> oldPairs;
            oldPairs.clear();
            string pair;
            
            int isFirstLine = 0;
      while (getline(oldfin, lineOld))
      {
      //////////////////////////////////////////
      // skip first line
      //////////////////////////////////////////
      if( isFirstLine == 0)
      {
                  isFirstLine = 1;
                  continue ;
      } // end of if

            string oldfn1 = GetField(lineOld, 0,',');
            string oldfn2 = GetField(lineOld, 1,',');
            string oldfn3 = GetField(lineOld, 2,',');
                        
            pair = oldfn1 + "," + oldfn2 + "," + oldfn3;
            oldPairs.insert( pair );
      } // end of while

      while (getline(newfin, lineNew))
      {
            string newfn1 = GetField(lineNew, 0,',');
            string newfn2 = GetField(lineNew, 1,',');
            string newfn3 = GetField(lineNew, 2,',');
             
            pair = newfn1 + "," + newfn2 + "," + newfn3;
            
            if (oldPairs.find(pair) == oldPairs.end() )
            {
                  fout << lineNew << endl;

            } // end of if
      } // end of while

      oldfin.close();
      newfin.close();
      fout.close();

      return 1;
}
0
justinY
Asked:
justinY
2 Solutions
 
rstaveleyCommented:
Assuming the first field is unique and that's how you can tell if you've got a new record, I'd make a map which uses the first field of oldfile.csv as the key and has a value made of of the pair of strings following. If then run through newfile.csv looking for the matching key in the map and comparing values if the key is found.
0
 
justinYAuthor Commented:
Thank you very much for your quick respond,but my problems are:
1.) all the fields can be changed.so, is it still good assuming first field is unique ?
2.) i still need to know how can I PUT "Y" in the front of newfn3 ?

Thanks
0
 
justinYAuthor Commented:
by the way, my current code is part of my whole program, so I really dont want to change it too much, unless I have no ways to accomplish my goal. Is there a way around to do it ? thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
efnCommented:
> 1.) all the fields can be changed.so, is it still good assuming first field is unique ?

No.

> 2.) i still need to know how can I PUT "Y" in the front of newfn3 ?

Just compose another string including the 'Y'.

string anotherLine =  newfn1 + "," + newfn2 + ",Y" + newfn3;

Then write out anotherLine instead of lineNew.
0
 
shah1d1698Commented:
OR,

If I were using C, I would have done it as follows...

                char newfn3[] = "333a";
      char *newfn3WithY;
      newfn3WithY = (char *) malloc( (strlen(st) + 1) );
      int i;
      newfn3WithY[0] = 'Y'; // put Y to the first position of newfn3WithY.
                //copy other chars to newfn3WithY from newfn3.
      for(i = 1; i < (strlen(st) + 1); i++) //start from i = 1,  as location i = 0 is occupied with Y.
      {
            newfn3WithY[i] = newfn3[i-1];
      }
      newfn3WithY[i] = '\0';
               //now print newfn3WithY on a teminal or write it to a file...
      printf("Resultant string : %s\n", newfn3WithY);
      
0
 
rstaveleyCommented:
Does any matching field indicate that it is a record requiring Y-treatment, or do you need to have two matching fields for Y-treatment?

0
 
justinYAuthor Commented:
Thanks guys,
let me back off a little bit. Lets assume first column is unique. then i do first column compare first.
1. if first column is same, then compare second and third columns, and write the difference to newfile and PUT Y in front of the first column (like flag)
2. if first column is different, then write the diffrenece to newfile.

thanks
0
 
rstaveleyCommented:
In that case I'd make a map with the first column as the key and the two other columns as the value.

e.g. using a std::pair<> to hold the values:

  typedef std::pair<std::string,std::string> ValuePair;
  typedef std::map<std::string,ValuePair> Map;

Its a bit late here in my time zone. Let me know if you need help with this approach and I can respond tomorrow, if someone else on your side of the pond doesn't get there first.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now