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输入文件如上所述,将其存储,按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
呼叫成功,但是第二个呼叫由于流已经打开而失败。
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么文件不是由 F 流创建的,或者即使它是输出只是垃圾值?
- Qt creator 4.11,在应用程序输出面板中创建一个链接
- 创建一个循环,该循环将输出大于零且小于 60(不包括 60)的所有 5 的倍数
- 从文件中读取多行.txt字符串删除空格并创建新文件进行输出
- 如何创建与OpenSSL C 输出相比以生成单元测试的OpenSSL输出
- 在张量流中创建新操作时指定形状句柄的形状(输出张量的秩问题)
- 如何创建一个具有输出 z 分数的概率输入的函数?(C++)
- 创建一个程序,该程序将一串数字作为输入,并输出偶数和奇数的总和
- C 循环未根据预期创建输出文件
- MinGW g++在powershell中不创建输出
- 为什么程序不会创建输出文件?(C++)
- 尝试使用freopen创建输出文件,使用非常量字符串
- 即使输入文件不存在,也会继续创建输出文件?编辑****
- 没有创建c++输出文件
- 创建/输出二叉树(不是BST)
- OpenCV创建输出流
- 创建输出文件 .rdstate() 时出错,返回值 2
- 代码块无法创建输出目录