#include "mapproc.h" #include #include #include //#include using namespace std; bool WriteFiles(map &m, char *pFileName); bool WriteFiles2(map &m, char *pFileName); typedef map::iterator mIT; bool ReadInputFile(map &m, char *pFileName) { int c=0; mIT it = m.begin(); ifstream infile; infile.open(pFileName); if (infile.fail()) { cout << "Error opening the file.\n"; return false; } while (!infile.eof() ) { char s[15]; infile.getline(s,15,'\t'); char *tmp; int k; k = strtol(s,&tmp,10); if(k>0) { for (it = m.begin(); (it != m.end()); ++it) { if(k==it->first) { c++; cout << "Duplicate ID: " << k << " stored in " << k + 2000 <<". eg. original ID + 2000\nTotal Duplicates found: " << c << "\n\n"; k=k+2000; } } char n[10]; infile.getline(n,10,'\n'); m[k]=n; } } infile.close(); return true; //foo = strtol(input, &tmp, 0); // if (tmp == input) { //// is not a number //} } bool OutputNumerically(map &m, char *pFileName) { /*mIT it = m.begin(); ofstream outfile; outfile.open(pFileName); if (outfile.fail()) { cout << "Error opening the file for writing.\n"; return false; } for (it = m.begin(); (it != m.end()); ++it) { outfile << it->first << "\t" << it->second << "\n"; } outfile.close(); return true;*/ if(WriteFiles(m,pFileName)) return true; else return false; } void SplitIntoStaffAndStudents(map &mStaff,map &mStudents, map &mAll) { mIT it = mAll.begin(); for (it = mAll.begin(); (it != mAll.end()); ++it) { if((it->first)/1000==1 || (it->first)/1000==3) { mStaff[it->second]=it->first; } } WriteFiles2(mStaff, "StaffList.txt"); for (it = mAll.begin(); (it != mAll.end()); ++it) { if((it->first)/1000==2 || (it->first)/1000==4) { mStudents[it->second]=it->first; } } WriteFiles2(mStudents, "StudentList.txt"); } bool OutputAlphabetically(map &m, char *pFileName) { mIT it = m.begin(); map mapAlpha; map::iterator itAlpha = mapAlpha.begin(); ofstream outfile; outfile.open(pFileName); if (outfile.fail()) { cout << "Error opening the file for writing.\n"; return false; } //int counter = 0; //counter was used to trouble shoot the for loop. for (it = m.begin(); (it != m.end()); ++it) { //counter++; if(mapAlpha.find(it->second)== mapAlpha.end()) { //cout << "if: " << counter << " \n"; mapAlpha[it->second] = (it->first); } else { //cout << "else: " << counter << "\n"; char buffer[256]; it->second += itoa(it->first,buffer,10); mapAlpha[it->second] = (it->first); } //cout << counter << "\n"; } for (itAlpha = mapAlpha.begin(); (itAlpha != mapAlpha.end()); ++itAlpha) { outfile << itAlpha->first << "\t" << itAlpha->second << "\n"; } outfile.close(); return true; } bool WriteFiles(map &m, char *pFileName) { mIT it = m.begin(); ofstream outfile; outfile.open(pFileName); if (outfile.fail()) { cout << "Error opening the file for writing.\n"; return false; } for (it = m.begin(); (it != m.end()); ++it) { outfile << it->first << "\t" << it->second << "\n"; } outfile.close(); return true; } bool WriteFiles2(map &m, char *pFileName) { mIT it = m.begin(); ofstream outfile; outfile.open(pFileName); if (outfile.fail()) { cout << "Error opening the file for writing.\n"; return false; } for (it = m.begin(); (it != m.end()); ++it) { outfile << it->first << "\t" << it->second << "\n"; } outfile.close(); return true; } // other function implementation here //outfile << it->first << "\t" << it->second << "\n";