Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文本从文件加载到二维数组(C+;+;)_C++_File_File Io_Fstream_Access Violation - Fatal编程技术网

C++ 将文本从文件加载到二维数组(C+;+;)

C++ 将文本从文件加载到二维数组(C+;+;),c++,file,file-io,fstream,access-violation,C++,File,File Io,Fstream,Access Violation,我正在制作一个游戏,我将地图数据存储在一个大小为[34][10]的二维数组中。最初,我使用一个简单的函数来填充数组,并使用以下代码将此数据保存到一个文件中: ofstream myFile; myFile.open("map.txt"); for ( int y = 0 ; y < MAP_HEIGHT ; ++y ) { for ( int x = 0 ; x < MAP_WIDTH ; ++x ) { myFile << m_acMa

我正在制作一个游戏,我将地图数据存储在一个大小为[34][10]的二维数组中。最初,我使用一个简单的函数来填充数组,并使用以下代码将此数据保存到一个文件中:

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();
流myFile的
;
打开(“map.txt”);
对于(int y=0;y
while ( myFile.getline(line, MAP_WIDTH) )
{
但是,读入std::字符串会更安全:

string line:
while ( getline( myFile, line ) )
{

你可能还想看看我关于这个主题的博客,在。

是的,我肯定会崩溃,因为你在这里做错了

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();

什么是
m_acMapData
,它是如何声明和初始化的?
string line:
while ( getline( myFile, line ) )
{
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();
for ( int y = 0 ; y < MAP_HEIGHT ; ++y ) 
{
    for ( int x = 0 ; x < MAP_WIDTH ; ++x )
    {
            myFile << m_acMapData[y][x]; // here to be changed
    }

    myFile << '\n';
}