我正在製作遊戲,並將地圖數據存儲在大小爲[34] [10]的二維數組中。本來我生成使用一個簡單的功能,填補了陣列的地圖,並使用下面的代碼此數據保存到一個文件:將文本從文件加載到二維數組中(C++)
ofstream myFile;
myFile.open("map.txt");
for (int y = 0 ; y < MAP_HEIGHT ; ++y)
{
for (int x = 0 ; x < MAP_WIDTH ; ++x)
{
myFile << m_acMapData[x][y];
}
myFile << '\n';
}
myFile.close();
它輸出一個文本文件,它看起來像我希望的方式。然而,當我嘗試重新使用下面的代碼讀它,我得到的訪問衝突的負載,它在運行時崩潰:
ifstream myFile;
int i=0;
int j=0;
char line[MAP_WIDTH];
myFile.open("map.txt");
while (!myFile.eof())
{
myFile.getline(line, MAP_WIDTH);
for (i=0; i<MAP_WIDTH; i++)
{
m_acMapData[i][j] = line[i];
}
++j;
cout << line;
}
有誰知道問題是什麼?
什麼'm_acMapData',它是如何聲明和初始化? – atzz 2009-12-09 10:25:04