C++ 循环未按预期创建输出文件

C++ Loop Not Creating Output Files as Expected

nm92,Nate,Matthews,Aetna,1
sc91,Steve,Combs,Cigna,2
ml94,Morgan,Lands,BCBS,3
kb93,Kyle,Borris,Aetna,2

我正在尝试像上面那样获取 CSV 输入文件,存储它,按保险排序(第 4 列),然后根据保险将其写入 diff 文件,但按姓氏的字母顺序排列。

你不需要阅读我的所有代码来帮助我坚持的最后一部分(文件输出),但我将它包括在内只是为了上下文。我已将 enrollVector[] 中的所有登记者按保险顺序排序,然后按姓氏排序,因此调用 sort() 后的打印语句如下所示:

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

如您所见,我的排序和一切似乎都工作正常(除了开头那个空的登记者),但将数据写入各自保险输出文件的最后一个循环似乎被打破了。它只为 Aetna 创建一个文件,并且确实以正确的字母顺序列出了正确的登记者,但没有创建其他包含其他登记者的保险文件。

为什么我的其他 insurance.csv 文件没有按照我认为的那样创建?

#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;
    }
}

您需要为每个文件创建一个单独的 ofstream 对象,或者,如果您选择重复使用同一对象,则需要先 close 一个文件再打开另一个文件。正如所写,第一次 open 调用成功,但第二次失败,因为流已经打开。