gpt4 book ai didi

c++ - 将 txt 文件加载到用逗号 C++ 分隔的 2 个暗淡数组值中

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:15 26 4
gpt4 key购买 nike

我需要这个来保存我正在制作的游戏的 map , 我使用此代码将数组保存到 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 除外)然后我想将其加载到 block 数组中。我需要用逗号分隔这些值,因为我要保存的一些 block ID 是多位数的。

我不知道如何在代码中实现这一点,尤其是逗号分隔,我做了很多研究但一无所获,所以我想我会问这个可爱的社区。


感谢所有帮助我使用此功能:

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

最佳答案

您可以告诉 getline 为下一个“行”使用自定义字符

std::ifstream file("data.txt");
std::string line;
while (std::getline(file, line))
{
std::istringstream ss(line);
std::string data;
while (std::getline(ss, data, ','))
{
// use data
}
}

关于c++ - 将 txt 文件加载到用逗号 C++ 分隔的 2 个暗淡数组值中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22750224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com