girija_cv
asked on
How to get not matched records?
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,123 4MN,RR
mno,@6654,4567XX,4567YY,SS
xyz,@5523,@5522,7890AA,789 0BB,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,CityP arms> CityParmsMap;
void process_a(CityParmsMap& map),process_b(CityParmsMa p& 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_iterat or Citr = Map.find(city);
if (Citr != Map.end()) {
CityParms& cityParms = Map[city];
cityParms.poco_list.push_b ack("@"+po co);
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_iterat or 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_li st.end(),s td::ostrea m_iterator <std::stri ng>(f,",") );
f << cityParms.x << ',';
f << cityParms.y << ',';
f << cityParms.adm /*<< ','*/;
f << '\n';
}
}
Can anyone help me?
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,123
mno,@6654,4567XX,4567YY,SS
xyz,@5523,@5522,7890AA,789
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,CityP
void process_a(CityParmsMap& map),process_b(CityParmsMa
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_iterat
if (Citr != Map.end()) {
CityParms& cityParms = Map[city];
cityParms.poco_list.push_b
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_iterat
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_li
f << cityParms.x << ',';
f << cityParms.y << ',';
f << cityParms.adm /*<< ','*/;
f << '\n';
}
}
Can anyone help me?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...