Reputation: 5
nm92,Nate,Matthews,Aetna,1
sc91,Steve,Combs,Cigna,2
ml94,Morgan,Lands,BCBS,3
kb93,Kyle,Borris,Aetna,2
I am trying to take a CSV input file like above, store it, sort it by insurance (col 4), and then write it to diff files based on insurance but in alphabetical order by last name.
You don't need to read all my code to help w/ the last part that I'm stuck on (the file output), but I included it just for context. I have all of the enrollees in enrollVector[] already sorted by insurance, then last name, so that the print statements after the call to sort() looks like below:
userid is:
fname is:
lname is:
insurance is:
version is:
userid is: kb93
fname is: Kyle
lname is: Borris
insurance is: Aetna
version is: 2
userid is: nm92
fname is: Nate
lname is: Matthews
insurance is: Aetna
version is: 1
userid is: ai90
fname is: Alex
lname is: Inter
insurance is: BCBS
version is: 4
userid is: ml94
fname is: Morgan
lname is: Lands
insurance is: BCBS
version is: 3
userid is: sc91
fname is: Steve
lname is: Combs
insurance is: Cigna
version is: 2
So as you can see, my sort and everything seems to be working fine (other than that empty enrollee at the beginning), but the final loop to write the data to their respective insurance output files seems to be broken. It ONLY creates a file for Aetna, and it does list the correct enrollees in the correct alphabetical order, but no other insurance files are created w/ the other enrollees in it.
Why are my other insurance.csv files not being created like I think they should?
#include <iostream>
#include <string> // for strings
#include <fstream> // for file streams
#include <vector>
#include <bits/stdc++.h> // for sort() implementation
using namespace std;
struct enrollee
{
string userid = "";
string fname = "";
string lname = "";
string insurance = "";
string version = "";
};
int main()
{
ifstream inputFile; // create input file stream for reading only
vector <enrollee> enrollVector; // array of structs to store each enrollee and their respective data
int vectorPos = 0;
// open the input file to read
inputFile.open("input.csv");
// read the file until we reach the end
while(!inputFile.eof())
{
enrollee tempEnrollee;
string userid = "";
string fname = "";
string lname = "";
string insurance = "";
string sversion = "";
// read in and store the cols of each row in a temp var
getline(inputFile,userid,',');
getline(inputFile,fname,',');
getline(inputFile,lname,',');
getline(inputFile,insurance,',');
getline(inputFile,sversion);
// assign those vars to an enrollee object
tempEnrollee.userid = userid;
tempEnrollee.fname = fname;
tempEnrollee.lname = lname;
tempEnrollee.insurance = insurance;
tempEnrollee.version = sversion;
// push the enrollee object onto the enrollVector
enrollVector.push_back(tempEnrollee);
// count how many enrollees we add for later po
vectorPos++;
}
// this call to sort will sort the enrollVector by insurance, then lname, then fname, then version
sort( enrollVector.begin(), enrollVector.end(), []( const enrollee &e1, const enrollee e2 )
{
return tie( e1.insurance, e1.lname, e1.fname, e1.userid, e1.version ) < tie( e2.insurance, e2.lname, e2.fname, e2.userid, e2.version );
});
for (int i = 0; i < vectorPos; i++)
{
cout << "userid is: " << enrollVector[i].userid << endl;
cout << "fname is: " << enrollVector[i].fname << endl;
cout << "lname is: " << enrollVector[i].lname << endl;
cout << "insurance is: " << enrollVector[i].insurance << endl;
cout << "version is: " << enrollVector[i].version << endl;
}
// set up output stream
string tempInsurance;
ofstream outputFile;
// write sorted data to their respective files
for (int i = 1; i < enrollVector.size() - 1; i++)
{
// if we come across a new insurance name, then start a new file for it
if (tempInsurance != enrollVector[i].insurance)
{
tempInsurance = enrollVector[i].insurance;
outputFile.open( tempInsurance + ".csv");
}
// write data to the file
outputFile << enrollVector[i].lname << "," << enrollVector[i].fname << ","
<< enrollVector[i].userid << "," << enrollVector[i].insurance << ","
<< enrollVector[i].version << endl;
}
}
Upvotes: 0
Views: 51
Reputation: 52471
You either need a separate ofstream
object for each file, or, if you choose to reuse the same object, you need to close
one file before opening another. As written, the first open
call succeeds, but the second fails since the stream is already open.
Upvotes: 2