files comparing questions

Posted on 2004-11-29
Last Modified: 2010-04-01
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;
Question by:justinY
    LVL 17

    Expert Comment

    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.

    Author Comment

    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 ?


    Author Comment

    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
    LVL 15

    Accepted Solution

    > 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.
    LVL 2

    Expert Comment


    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);
    LVL 17

    Expert Comment

    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?


    Author Comment

    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.

    LVL 17

    Assisted Solution

    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.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now