我是使用C++進行編程的新手。我想寫一些數據到一個CSV文件。 這是我的代碼,試圖做到這一點,但它只寫1列( 而不是另一(年)列1變量(人口)。將數據從C++寫入csv
#include <fstream>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
/data generation/
ofstream USPopulation;
USPopulation.open("D:\\2.csv");
USPopulation << "Population,Year" << endl;
int year = 1790;
for (int index = 0; index < count; index++)
{
USPopulation << population[index], year;/this only writes the population values/
USPopulation << endl;
year += 1;
}
USPopulation.close();
return 0;
}
有人可以告訴我爲什麼它只是寫入文件的人口值而不是年份? 謝謝!
',year;'<===你打算怎麼做? –
試試'<<'操作符:'USPopulation << population [index] << year;'。因爲你想要一個CSV文件'USPopulation << population [index] <<「,」<< year << std :: endl' –