【发布时间】:2014-03-30 21:39:44
【问题描述】:
我需要这个来保存我正在制作的游戏的地图, 我使用此代码将数组保存到 txt 文件:
void saveMap(string name){
ofstream myFile;
myFile.open(name.c_str());
for (int y = 0; y < 100; ++y){
for (int x = 0; x < 257; ++x){
myFile << blocks[x][y].get() << ",";
}
myFile << '\n';
}
myFile.close();
}
所以我最终会得到类似的东西:
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,1,1,0,0,1,1,0,
0,1,1,0,0,1,1,0,
0,0,0,2,2,0,0,0,
0,0,2,2,2,2,0,0,
0,0,2,2,2,2,0,0,
0,0,2,0,0,2,0,0,
(地形类和 257 x 100 除外) 然后我想将它加载到块数组中。 我需要用逗号分隔这些值,因为我要保存的一些块 ID 是多位的。
我不知道如何在代码中实现这一点,尤其是在逗号分隔的情况下,我做了很多研究但一无所获,所以我想我会问这个可爱的社区。p>
感谢所有帮助我使用此功能:
void loadMap(string name){
std::ifstream file(name.c_str());
std::string line;
int i=0,j=0;
while (std::getline(file, line)){
std::istringstream ss(line);
std::string data;
while (std::getline(ss, data, ',')){
blocks[i][j].set(atoi(data.c_str()),1,true);
i++;
}
i=0;
j++;
}
}
【问题讨论】:
标签: c++ arrays text-files ifstream