C 循环未根据预期创建输出文件

C++ Loop Not Creating Output Files as Expected

本文关键字:创建 输出 文件 循环      更新时间:2023-10-16
nm92,Nate,Matthews,Aetna,1
sc91,Steve,Combs,Cigna,2
ml94,Morgan,Lands,BCBS,3
kb93,Kyle,Borris,Aetna,2

我试图将CSV输入文件如上所述,将其存储,按Insurance(Col 4(进行排序,然后根据保险将其写入diff文件,但按字母顺序按姓氏。

您不需要读取我的所有代码即可帮助我坚持使用的最后一部分(文件输出(,但我仅将其包含在上下文中。我有所有已经按保险进行排序的注册者的注册人,然后是姓氏,以便在调用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创建一个文件,并且确实以正确的字母顺序列出了正确的注册人,但没有创建其他保险文件。

为什么我的其他保险。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呼叫成功,但是第二个呼叫由于流已经打开而失败。