C++读CSV文件,处理数据
//读写
#include "global.h"
#include <iostream>
#include <fstream> //读取文件数据的函数fin的头文件
#include <sstream> //istringstream 必须包含这个头文件
using namespace std;
using std::ofstream;
vector<AQIvalue> allSites;
vector<WENDUvalue> allWD;
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string str){
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int ReadWENDU(const string siteFile)
{
string value;
ifstream fin;
fin.open(siteFile, ios::in);
if (!fin.is_open())
{
cout << "read file error: " << siteFile << endl;
}
getline(fin, value); //读取文件的一行
int cnt = 0;
while (fin.good())//判断文件是否打开
{
cnt++;
string Anum,Aname,Acity,Ajindu,Aweidu,WENDU1, SHIDU1, FENGSU1, WENDU2, SHIDU2, FENGSU2, WENDU3, SHIDU3, FENGSU3, WENDU4, SHIDU4, FENGSU4 ;
//string WENDU1, WENDU2, WENDU3, WENDU4, SHIDU1, SHIDU2, SHIDU3, SHIDU4, FENGSU1, FENGSU2, FENGSU3, FENGSU4;
WENDUvalue B_SITE;
getline(fin, Anum, ',');
B_SITE.Anum = Anum;
getline(fin, Aname, ',');
B_SITE.Aname = Aname;
getline(fin, Acity, ',');
B_SITE.Acity = Acity;
getline(fin, Ajindu, ',');
B_SITE.Ajindu = stringToNum<float>(Ajindu);
getline(fin, Aweidu, ',');
B_SITE.Aweidu = stringToNum<float>(Aweidu);
getline(fin, WENDU1, ',');
B_SITE.wendu3 = stringToNum<float>(WENDU1);
getline(fin, WENDU2, ',');
B_SITE.shidu3 = stringToNum<float>(WENDU2);
getline(fin, WENDU3, ',');
B_SITE.fengsu3 = stringToNum<float>(WENDU3);
getline(fin, SHIDU1, ',');
B_SITE.wendu4 = stringToNum<float>(SHIDU1);
getline(fin, SHIDU2, ',');
B_SITE.shidu4 = stringToNum<float>(SHIDU2);
getline(fin, SHIDU3, '\n');
B_SITE.fengsu4 = stringToNum<float>(SHIDU3);
allWD.push_back(B_SITE);
}
fin.close();
fin.clear();
return 0;
}
int ReadAQI(const string siteFile)
{
string value;
ifstream fin;
fin.open(siteFile, ios::in);
if (!fin.is_open())
{
cout << "read file error: " << siteFile << endl;
}
//getline(fin, value); //读取文件的一行
int cnt = 0;
while (fin.good())//判断文件是否打开
{
cnt++;
//cout << "cntAQI: " << cnt << endl;//总共多少个数
//system("pause");
string v1, v2, v3, v4,v5,v6;
AQIvalue A_SITE;
getline(fin, v1, ',');
A_SITE.value1 = stringToNum<float>(v1);
getline(fin, v2, ',');
A_SITE.value2 = stringToNum<float>(v2);
getline(fin, v3, ',');
A_SITE.value3 = stringToNum<float>(v3);
getline(fin, v4, ',');
A_SITE.value4 = stringToNum<float>(v4);
getline(fin, v5, ',');
A_SITE.value5 = stringToNum<float>(v5);
getline(fin, v6, '\n');
A_SITE.value6 = stringToNum<float>(v6);
allSites.push_back(A_SITE);
}
fin.close();
fin.clear();
return 0;
}
int main(int argc, char* argv[])
{
cout << "fuck";
ReadWENDU("site_Temperature_SpeHumidity_2015_1_1_6.csv");
int biaoji[1500];
float outdata[2000][2000];
for (int i = 0; i < 1497; i++)
{
biaoji[i] = 0; //存取城市之间距离
}
for (int i = 0; i < 1497; i++)
{
for (int j = 0; j < 1497; j++)
{
outdata[i][j] = sqrtf((allWD[i].Ajindu-allWD[j].Ajindu)*(allWD[i].Ajindu - allWD[j].Ajindu)+ (allWD[i].Aweidu - allWD[j].Aweidu)*(allWD[i].Aweidu - allWD[j].Aweidu)); //存取城市之间距离
}
}
//ofstream outFile;
//outFile.open(data, ios::out);
//for (int i = 0; i < wholenum; i++)
//{
// for (int j = 1; j < 6; j++)
// {
// outFile << data[i][j] << ',';
// }
// outFile << data[i][6] << endl;
//}
//outFile.close();
allWD.clear();
allSites.clear();
return 0;
}