用于将文件中的字符串读取到 3D 数组中的 For 循环

发布于 2024-11-05 06:46:29 字数 1789 浏览 0 评论 0原文

我的一个函数遇到问题,我正在开发一个简单的图块地图编辑器,并且我正在尝试实现一个 3D 数组来跟踪图块(x,y,图层)。在此之前,我有一个一维数组,其中所有图块都按顺序列出:

bool Map::OnLoad(char* File) {
    TileList.clear();
    FILE* FileHandle = fopen(File, "r");
    if(FileHandle == NULL) {
        return false;
    }

    for(int Y = 0;Y < MAP_HEIGHT;Y++) {
        for(int X = 0;X < MAP_WIDTH;X++) {
            Tile tempTile;

            fscanf(FileHandle, "%d:%d ", &tempTile.TileID, &tempTile.TilePassage);

            TileList.push_back(tempTile);
        }
        fscanf(FileHandle, "\n");
    }
    fclose(FileHandle);
    return true;
}

这基本上从文件中读取字符串,如下所示:

2:1 1:0 3:2...

其中第一个数字表示图块 ID,第二个数字表示图块可通过性。 以上功能有效。我的 3D 数组也已正确构造,我通过简单的赋值并从中调用值来测试它们。 给我带来问题的函数如下(请注意,添加了数字 2,即 OnLoad2(),这样我就可以保持旧变量和函数不变,直到原型正常工作)

bool Map::OnLoad2(char* File) {
    TileList2.clear();
    FILE* FileHandle2 = fopen(File, "r");
    if(FileHandle2 == NULL) {
        return false;
    }

    for(int Y = 0;Y < MAP_HEIGHT;Y++) {

        for(int X = 0;X < MAP_WIDTH;X++) {
            Tile tempTile;
            fscanf(FileHandle2, "%d:%d ", &tempTile.TileID, &tempTile.TilePassage);

            TileList2[X][Y][0] = tempTile;
        }
        fscanf(FileHandle2, "\n");
    }
    fclose(FileHandle2);
    return true;
}

:不会触发编译器报告任何错误,应用程序一启动,它就会冻结并崩溃。有关其他信息,MAP_WIDTH 和 MAP_HEIGHT 分别设置为 40,3D 数组的构造如下:

TileList2.resize(MAP_HEIGHT);
for (int i = 0; i < MAP_HEIGHT; ++i) {
    TileList2[i].resize(MAP_WIDTH);
    for (int j = 0; j < MAP_WIDTH; ++j)
        TileList2[i][j].resize(3);
}

如果您能指出我需要修复什么,据我所知,我将不胜感激当 3D 数组初始化并正常工作时,肯定搞乱了 for 循环结构。感谢您的帮助!

I'm having a problem with one of my functions, I'm working on a simple tile map editor, and I'm trying to implement a 3D array to keep track of tiles (x,y, layer). Before this I had a 1D array where all the tiles were just listed sequencially:

bool Map::OnLoad(char* File) {
    TileList.clear();
    FILE* FileHandle = fopen(File, "r");
    if(FileHandle == NULL) {
        return false;
    }

    for(int Y = 0;Y < MAP_HEIGHT;Y++) {
        for(int X = 0;X < MAP_WIDTH;X++) {
            Tile tempTile;

            fscanf(FileHandle, "%d:%d ", &tempTile.TileID, &tempTile.TilePassage);

            TileList.push_back(tempTile);
        }
        fscanf(FileHandle, "\n");
    }
    fclose(FileHandle);
    return true;
}

This basically read strings from the file which looked like:

2:1 1:0 3:2...

Where the first number states the tileID and the second one states the Tile passability. The above function works. My 3D arrays are also correctly constructed, I tested them with simple assignments and calling values out of it. The function that gives me problems is the following (please note that the number 2 i.e. OnLoad2() was added so I can keep the old variables and the function untouched until the prototype is working):

bool Map::OnLoad2(char* File) {
    TileList2.clear();
    FILE* FileHandle2 = fopen(File, "r");
    if(FileHandle2 == NULL) {
        return false;
    }

    for(int Y = 0;Y < MAP_HEIGHT;Y++) {

        for(int X = 0;X < MAP_WIDTH;X++) {
            Tile tempTile;
            fscanf(FileHandle2, "%d:%d ", &tempTile.TileID, &tempTile.TilePassage);

            TileList2[X][Y][0] = tempTile;
        }
        fscanf(FileHandle2, "\n");
    }
    fclose(FileHandle2);
    return true;
}

While this function doesn't trigger the compiler to report any errors, as soon as the application starts, it freezes up and crashes. For additional information MAP_WIDTH and MAP_HEIGHT are set to 40 each and the 3D array was constructed like this:

TileList2.resize(MAP_HEIGHT);
for (int i = 0; i < MAP_HEIGHT; ++i) {
    TileList2[i].resize(MAP_WIDTH);
    for (int j = 0; j < MAP_WIDTH; ++j)
        TileList2[i][j].resize(3);
}

I would appreciate it if you could point me out what do I need to fix, as far as I know I must have messed up the for loop structure, as the 3D array initializes and works properly. Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-11-12 06:46:29
TileList2.clear();

此行重新初始化 TileList2,因此它返回到零长度向量。删除该行,您可能会没事。

TileList2.clear();

This line reinitializes TileList2, so it is back to a zero-length vector. Delete that line, and you will probably be okay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文