files comparing questions

Hi experts,
I have the 2 files as followings:



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:


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 )
    return field;

int main(int argc, char *argv[])
            ifstream oldfin;
            ifstream newfin;
            ofstream fout;

            string lineOld;
            string lineNew;
            set<string> oldPairs;
            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


      return 1;
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
justinYAuthor Commented:
Thank you very much for your quick respond,but my problems are:
1.) all the fields can be, 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 ?

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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

> 1.) all the fields can be, 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 ?

Just compose another string including the 'Y'.

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

Then write out anotherLine instead of lineNew.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

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);
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?

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.

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.