Solved

How to get not matched records?

Posted on 2004-03-27
4
299 Views
Last Modified: 2010-04-17
pqr,1234PQ,1234MN
xyz,7890AA,7890BB
mno,4567XX,4567YY
zzz,1111AA,2222BB
first field is city second field is x & third is y.

Contents of b.txt are as follows

abc,8899,TT
abc,8898,TT
abc,8897,TT
pqr,7766,RR
pqr,7765,RR
mno,6654,SS
xyz,5523,PP
xyz,5522,PP
first field is city second field is poco & third is adm.if city in b.txt matches with a.txt I want print the output as follows

pqr,@7766,@7765,1234PQ,1234MN,RR
mno,@6654,4567XX,4567YY,SS
xyz,@5523,@5522,7890AA,7890BB,PP

If city in b.txt does not exist in a.txt I want to write those records in seperate file.How to do this in c++?

The coding is as follows

#include <iostream>
#include <sstream>
#include <fstream>
#include <list>
#include <map>
#include <algorithm>

struct CityParms {
        std::string x,y,adm;
        std::list<std::string> poco_list;
        CityParms(const std::string& x = "",const std::string &y = "") : x(x),y(y) {}
};
typedef std::map<std::string,CityParms> CityParmsMap;

void process_a(CityParmsMap& map),process_b(CityParmsMap& map),report(const CityParmsMap& map);

int main()
{
        CityParmsMap map;
        process_a(map);
        process_b(map);
        report(map);
}

void process_a(CityParmsMap& map)
{
        std::ifstream f("a.txt");
        std::string str;
        while (getline(f,str)) {
                std::istringstream istr(str);
                std::string city,x,y;
                if (getline(istr,city,',') && getline(istr,x,',') && getline(istr,y,','))
                        map[city] = CityParms(x,y);
        }
}

void process_b(CityParmsMap& Map)
{
        std::ifstream f("b.txt");
        std::string str;
        while (getline(f,str)) {
                std::istringstream istr(str);
                std::string city,poco,adm;
                if (getline(istr,city,',') && getline(istr,poco,',') && getline(istr,adm,',')) {
                        CityParmsMap::const_iterator Citr = Map.find(city);
                        if (Citr != Map.end()) {
                                CityParms& cityParms = Map[city];
                                cityParms.poco_list.push_back("@"+poco);
                                cityParms.adm = adm;
                        } // Here what should be the condition if city is not found...
                }
        }
}

void report(const CityParmsMap& Map)
{
        std::ofstream f("report.csv");
        typedef CityParmsMap::const_iterator Citr;
        for (Citr itr = Map.begin();itr != Map.end();++itr) {
                const CityParms& cityParms = itr->second;
                const std::list<std::string> &poco_list = cityParms.poco_list;
                if (!poco_list.size())
                        continue;
                f << itr->first << ',';
                copy (poco_list.begin(),poco_list.end(),std::ostream_iterator<std::string>(f,","));
                f << cityParms.x << ',';
                f << cityParms.y << ',';
                f << cityParms.adm /*<< ','*/;
                f << '\n';
        }
}

Can anyone help me?
0
Comment
Question by:girija_cv
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
gmleeman earned 20 total points
ID: 10697705
An elegant way is to load both files into 2 arrays, keeping the city text in alphabetical order - use insertion sorting perhaps, or assume they're already in alpha order.

Treat one file as your master (the smaller file perhaps)

and use your other file as the search file.

Then use a Binary search to search for every item contained in master for its matching city in the search file, report on every city you do not find. (You must keep the "search" file in alpha order to use a binary search).  A binary search is very efficient (on lists no bigger than 10000 records) and easy to code. (Also called a bi-ficating search)

It seems like you have the skill to write this, let me know if you need a hand with Sorting or Searching.

You may swap "master" (a.txt) with "search" (b.txt) file, depending on their size and the speed you need in this process.
0
 
LVL 3

Expert Comment

by:gmleeman
ID: 10697710
Sorry - doubly linked lists and vectors, etc are just as elegant.  But "array" of some form is the theory I meant.  You may also use the file pointers instead of loading anything, but prone to disaster...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

20 Experts available now in Live!

Get 1:1 Help Now