C + + read CSV file, processing data

Source: Internet
Author: User

Write

#include "global.h"
#include <iostream>
#include <fstream>//function of reading file data fin header file
#include <sstream>//istringstream must contain this header file

using namespace Std;
Using Std::ofstream;
Vector<aqivalue> allsites;
Vector<wenduvalue> ALLWD;


Template functions: Converting a string variable to a common numeric type (this method is universally applicable)
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); Read a line of files

int cnt = 0;

while (Fin.good ())//Determine if the file is open
{
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); Read a line of files

int cnt = 0;

while (Fin.good ())//Determine if the file is open
{
cnt++;
cout << "Cntaqi:" << cnt << endl;//Total number
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; Distance between access cities
}

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)); Distance between access cities
}
}


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

C + + read CSV file, processing data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.