使用 fopen 写入 VS2015 中的文件时发生访问冲突

Acess violation upon using fopen to write to a file in VS2015

所以我会很快完成。我正在尝试 运行 以前在代码块中工作的代码,但出于个人原因我真的想让它在 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。 特别 Headers 我正在使用:

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

我该如何解决这个问题?

你正试图写信给 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;
}