這裏有一段代碼,我用它來從data.csv
文件導入我的數據,到24個變量:導入選項卡和換行分隔的CSV數據到變量
#include <iostream>
#include <fstream>
using namespace std;
double xa = 0;
double ya = 0;
double Ta = 0;
double Ca = 0;
double Ma = 0;
double Da = 0;
double xb = 0;
double yb = 0;
double Tb = 0;
double Cb = 0;
double Mb = 0;
double Db = 0;
double xc = 0;
double yc = 0;
double Tc = 0;
double Cc = 0;
double Mc = 0;
double Dc = 0;
double xd = 0;
double yd = 0;
double Td = 0;
double Cd = 0;
double Md = 0;
double Dd = 0;
//__________________________________________________
int trash = 0; // ifstream bug (?) stuff
//___________________________________
int main() {
ifstream ifs ("data.csv"); ///LOADING
if (!ifs)
// process error
ifs >> trash;
ifs >> xa ;
ifs >> ya ;
ifs >> Da ;
ifs >> Ma ;
ifs >> Ca ;
ifs >> Ta ;
ifs >> xb ;
ifs >> yb ;
ifs >> Db ;
ifs >> Mb ;
ifs >> Cb ;
ifs >> Tb ;
ifs >> xc ;
ifs >> yc ;
ifs >> Dc ;
ifs >> Mc ;
ifs >> Cc ;
ifs >> Tc ;
ifs >> xd ;
ifs >> yd ;
ifs >> Dd ;
ifs >> Md ;
ifs >> Cd ;
ifs >> Td ;
當然(我猜... ,)它只能使用CSV結構(每行包含一個新的單一數據:)
63.64474122
5.21472834
0.40511019
0.26155648
0.00000000
0.00000000
34.45902482
15.62249852
0.22086168
0.00000000
0.44580498
0.00000000
82.11020306
14.02709406
0.50748794
0.00000000
0.00000000
0.15917872
0.00000000
0.00000000
0.00000000
0.00000000
0.00000000
0.00000000
出於顯而易見的實際原因,我想用這個文件結構爲CSV(製表和newlines-分開):
63,64474122 5,21472834 0,40511019 0,26155648 0,00000000 0,00000000
34,45902482 15,62249852 0,22086168 0,00000000 0,44580498 0,00000000
82,11020306 14,02709406 0,50748794 0,00000000 0,00000000 0,15917872
0,00000000 0,00000000 0,00000000 0,00000000 0,00000000 0,00000000
可能有人幫助我嗎?
對於這個例子中的逗號感到抱歉,我的意思是浮點數! –