c++ - 如何将数据作为额外列写入 .csv 文件
问题描述
我有一个这样的csv文件
NAME, BANK ACCOUNT, SORT CODE, INVESTMENT
A 123 XXX 1000
B 456 XXX 2000
C 789 XXX 3000
我想添加一个额外的列,使文件看起来像
NAME, BANK ACCOUNT, SORT CODE, INVESTMENT, Contribution
A 123 XXX 1000 16.67
B 456 XXX 2000 33.33
C 789 XXX 3000 50
我已经制定了一个float percentage
包含所有必需元素的方法,现在我对如何将它作为额外的行添加到数据文件中感到困惑。
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>
using namespace std;
#define W1 0.2
vector<float>Data_investment;
string Trim(string& str);
string Trim(string& str)
{
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}
void Get_Data(const string& s, vector<float>& data)
{
ifstream fin(s);
string line;
int flag = 0;
while (getline(fin, line))
{
if (flag)
{
istringstream sin(line);
vector<string> fields;
string field;
while (getline(sin, field, ','))
{
fields.push_back(field);
}
string investment = Trim(fields[3]);
data.push_back(atof(const_cast<const char*>(investment.c_str())));
}
++flag;
}
}
int main()
{
Get_Data("aaa.csv", Data_investment);
int sum = 0;
for (auto i : Data_investment) {
sum += i;
}
float total = accumulate(Data_investment.begin(), Data_investment.end(), 0);
const unsigned int quantity = Data_investment.size();
for (unsigned int i = 0; i < quantity; ++i)
{
float percentage = (Data_investment[i] * 100.0f) / total;
cout << percentage << ", ";
}
cout << endl;
return 0;
}
解决方案
尝试这样的事情:
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>
using namespace std;
struct Investment
{
string Name;
string BankAccount;
string SortCode;
float Investment;
float Contribution;
};
string Trim(string str)
{
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}
void Get_Data(const string &filename, vector<Investment> &data)
{
ifstream fin(filename);
if (!fin.is_open())
return;
string line;
if (!getline(fin, line))
return;
while (getline(fin, line))
{
istringstream sin(line);
vector<string> fields;
string field;
while (getline(sin, field, ','))
{
fields.push_back(field);
}
Investment inv;
inv.Name = Trim(fields[0]);
inv.BankAccount = Trim(fields[1]);
inv.SortCode = Trim(fields[2]);
inv.Investment = atof(Trim(fields[3]).c_str());
inv.Contribution = 0.0f;
data.push_back(inv);
}
}
void Save_Data(const string &filename, const vector<Investment> &data)
{
ofstream fout(filename);
if (!fout.is_open())
return;
fout << "NAME,BANK ACCOUNT,SORT CODE,INVESTMENT,Contribution\n";
for (auto &inv : data)
{
fout << inv.Name << ","
<< inv.BankAccount << ","
<< inv.SortCode << ","
<< inv.Investment << ","
<< inv.Contribution << "\n";
}
}
int main()
{
vector<Investment> Data_investment;
Get_Data("aaa.csv", Data_investment);
float total = accumulate(Data_investment.begin(), Data_investment.end(), 0.0f,
[](float sum, const Investment &inv){ return sum + inv.Investment; }
);
for (auto &inv : Data_investment)
{
inv.Contribution = (inv.Investment * 100.0f) / total;
}
Save_Data("aaa_new.csv", Data_investment);
return 0;
}
推荐阅读
- sql - Laravel 5.8 同一张表上的外部关系
- vaadin - 使用 CSS 在标签文本右侧显示星号 (*)
- c# - 从支付网关重定向到 ASPX 时,Request.Form.Count 始终为 0
- java - 项目在 aws-lambda 上时如何以编程方式增加 jvm 堆大小
- c# - 我需要帮助使用 MS 调试诊断工具来分析 CPU 使用率高的 C# 应用程序
- javascript - Redux 拆分 reducers - 在一个文件中导出多个
- acumatica - 将扩展的 Web 服务端点升级到系统合同 3 (18.200.001) 会引发错误“查看结果具有 BQL 委托”
- angular - cdkDragMoved 事件返回pointerPosition 基于clientX 和clientY 而不是容器
- amp-html - AMP-carousel 所需高度
- angular - CanActivateChild 未运行