我发现你需要使用,
来分隔你想写的单元格,但它对我来说不起作用。
这是代码:
fileName "example_1.csv"
void World::saveDataToFile(std::string fileName){
std::ofstream myfile(fileName);
if (myfile.is_open())
{
for (int index = 0; index < dataList.size(); index++){
myfile << dataList[index].ants << "," << dataList[index].beetles << "," << dataList[index].empty << "\n";
}
myfile.close();
}
else std::cout << "Unable to open file";
}
它只是使用逗号在同一单元格中写入。我找不到任何其他解决方案。
我怎样才能获得下一个细胞?
EDITED
错误的电子表格:
// std::vector< std::vector <Organism* > > world; // wrong vector
std::vector<Data > dataList; // this is the right one!
struct Data{
Data(int a, int b, int e) : ants(a), beetles(b), empty(e){}
int ants;
int beetles;
int empty;
};
涉及的职能:
void World::saveData(){
dataList.push_back(Data(countOrganismType(ANT),countOrganismType(BEETLE),countOrganismType(NONE)));
}
int World::countOrganismType(Organism* org){
int count = 0;
for (int x = 0; x < wSizeX; x++){
for (int y = 0; y < wSizeY; y++){
if (compare(world[x][y], org)){
count++;
}
}
}
return count;
}