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

时间:2019-07-01 03:36:31

标签: c++ file-io

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

1 个答案:

答案 0 :(得分:2)

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