-2

所以我会很快做到这一点。我试图运行以前在代码块中工作的代码,但出于个人原因,我真的想让它在 Visual Studio 中工作。这是错误:

Exception thrown at 0x77EB9F83 (ntdll.dll) in bgi.exe: 0xC0000005: Access violation writing location 0x73EC21E0.

If there is a handler for this exception, the program may be safely continued.

这是我的代码:

#include "graphics.h"
//#include <Windows.h>
#include <math.h>
#include <stdio.h>

void drawGridOnX(int xtotal, int ytotal);
int levelcode[400][45][1];
void decodelevelAndDraw(int minx,int maxx);
void saveCurrentLevel();

void main() {
    initwindow(1600, 900,"Testscreen",0,0,true,true);
    int x=0,y=0,xmin=0,xmax=23,cellx,celly,cellSize=70, xtotal = 0, ytotal = 0,counter=0;
    // gridposx = 0, gridposy = 0, diffx = 0, diffy = 0, distanceFromMouse = 40, titlenumberx = 0, titlenumbery = 0,

    while (1) {
        setbkcolor(9);
        cleardevice();
        ytotal = 0;
        /*diffx = mousex() - gridposx;
        while (gridposx < mousex()&&diffx>=70) {
            gridposx += 70;

        }
        while (gridposx > mousex()&&diffx<=-70 + distanceFromMouse) {
            gridposx =gridposx-70;

        }
        diffy = mousey() - gridposy;
        while (gridposy < mousey() && diffy >= 70) {
            gridposy += 70;

        }
        while (gridposy > mousey() && diffy <= -70+distanceFromMouse) {
            gridposy = gridposy - 70;

        }
        */
        cellx = std::floor(mousex() / cellSize);
        celly = std::floor(mousey() / cellSize);
        while (ytotal < 900) {
            drawGridOnX(xtotal, ytotal);
            ytotal += 70;
        }

        if(GetAsyncKeyState(VK_RETURN)){
            saveCurrentLevel();

        }else if (ismouseclick(WM_LBUTTONDOWN)) {
                if (ismouseclick(WM_LBUTTONUP)) {

                    getmouseclick(WM_LBUTTONUP, x, y);
                    getmouseclick(WM_LBUTTONDOWN, x, y);
                }
                //cellx = gridposx / 70;
                //celly = gridposy / 70;
                cellx += xmin;
                levelcode[cellx][celly][0]=1;
                //printf("CLICK");
            }else if (ismouseclick(WM_RBUTTONDOWN)) {
                    if (ismouseclick(WM_RBUTTONUP)) {

                        getmouseclick(WM_RBUTTONUP, x, y);
                        getmouseclick(WM_RBUTTONDOWN, x, y);
                    }
                    //cellx = gridposx / 70;
                    //celly = gridposy / 70;
                    cellx += xmin;
                    levelcode[cellx][celly][0] = 0;
                    //printf("CLICK");
                }else if (GetAsyncKeyState(0x27)) {
                        //printf("RIGHT\n\n\n\n");
                        Sleep(100);
                        xmin++;
                        xmax++;
                    }else if (GetAsyncKeyState(0x25)&&xmin!=0) {
                        //printf("RIGHT\n\n\n\n");
                        Sleep(100);
                        xmin--;
                        xmax--;
                }
        decodelevelAndDraw(xmin,xmax);
        readimagefile("question_blueprint.jpg", cellx*70,celly*70, 70+cellx*70, 70+celly*70);
        //settextstyle(SANS_SERIF_FONT,1);
        settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 1);
        outtextxy(0, 0, "Press Enter to save");
        //printf("gridposx:%d\tgridposy:%d\ttitlenumberx:%d\ttitlenumbery%d",gridposx,gridposy,gridposx/70,gridposy/70);
        swapbuffers();

    }
}


void drawGridOnX(int xtotal, int ytotal) {
    while (xtotal < 1600) {
        rectangle(xtotal, ytotal, 70 + xtotal, 70+ytotal);
        xtotal += 70;

    }


}


void decodelevelAndDraw(int minx,int maxx) {
    int x = 0, y = 0;
    while (y != 13) {
        while (x != maxx) {
            if (levelcode[x][y][0] == 1) {
                //x -= minx;
                readimagefile("question.jpg", x*70-minx*70, y*70, 70 + x*70-minx*70, 70 + y*70);
                //printf("Block added at %d;%d", x * 70, y * 70);
            }
            x++;
        }
        x = 0;
        y++;
    }
}


void saveCurrentLevel() {
    int x = 0, y = 0, z = 0;
    FILE *fp;
    fopen("map.txt","w+");
    while (y < 13) {


        while (x < 400) {
            fprintf(fp, "%d ", levelcode[x][y][z]);
            x++;
        }
        printf("\n");
        x = 0;

        y++;
    }



}

我使用的是 graphics.h 的 winbgim 版本。我正在使用 win 10。我使用的特殊标题:

dibutil.h
graphics.h
winbgi.h
wingim.h
winbgitypes.h

如果您还需要什么,请告诉我,我会提供。

我怎样才能解决这个问题?

提前致谢。

4

1 回答 1

3

当你有这个时,你正试图写信fp

FILE *fp;
fopen("map.txt","w+");

相反,它应该是:

FILE *fp;
fp = fopen("map.txt","w+");

要不就:

FILE *fp = fopen("map.txt","w+");

此外,您应该在开始写入文件之前检查文件是否打开,例如,如果没有,您可以打印错误消息并退出您的函数:

if ( fp == NULL ) {
    printf ("File is not open\n");
    return;
}
于 2016-03-31T19:09:04.847 回答