Why is the wrong address saved in API file input/output?

19 views Asked by At

Situation:

  1. Using API dialog. When you left-click the mouse, a dialog is created, where you decide on the value to be saved to the file, and when you press the 'OK' button, it is saved to the binary file. And the dialog closes.

  2. After the dialog box closes, right-click to read the saved file.

Problem: I have no problem reading the file as soon as it is first created. However, in OpenFile(), the following error appears after the function is completed.

Exception occurred (0x00403CEC, example.exe): 0xC0000005: An access violation occurred while reading location 0x0081F674. An unhandled exception was thrown: Read access violation. _Pnext was 0x5A1CE4.

I know that that error occurs when you try to access a memory area that you don't have permission to access. Why does this problem occur? The code is below.

typedef struct
{
    int ID_Name;
    int ID_Emotiation;
    string str;
}CCharacter_Script;

void myDebugMsg(const char* format, ...)
{
#ifdef _DEBUG
    char str[128];
    va_list args;
    va_start(args, format);
    vsprintf(str, format, args);
    OutputDebugString(str);
    va_end(args);
#endif
}

void Save(int nameID, int emotationID, char script[])
{
    CCharacter_Script character;
    character.ID_Name = nameID;
    character.ID_Emotiation = emotationID;
    character.str = script;

    FILE* fp, * op;

    fp = fopen("Save_Script.bin", "rb");

    if (fp == NULL)
    {
        fp = fopen("Save_Script.bin", "wb");
        fwrite(&character, sizeof(CCharacter_Script), 1, fp);
        fclose(fp);
    }
    else
    {
        fclose(fp);

        op = fopen("Save_Script.bin", "ab");
        fwrite(&character, sizeof(CCharacter_Script), 1, op);
        fclose(op);
    }
}

void OpenFile()
{
    CCharacter_Script character;
    FILE* fp;
    int r_count;

    fp = fopen("Save_Script.bin", "rb");

    if (fp == NULL)
    {
        OutputDebugString("Error :: None File\n");
    }
    else
    {
        OutputDebugString("Reading :: Script\n");
        // Continue repeating until you reach the end of the file
        while ((r_count = fread(&character, sizeof(CCharacter_Script), 1, fp)) > 0)
        {
            myDebugMsg("Name: %d    Emotation: %d\n '%s'\n", character.ID_Name, character.ID_Emotiation, character.str.c_str());
        }
        fclose(fp);
    }
}
1

There are 1 answers

4
Rajnikant911 On

In the 'save()' function, you are trying to open the file in 'append binary mode' for writing, but you are using the pointer 'fp' instead of 'op' after closing the initial file pointer.

try this save() function instead

void Save(int nameID, int emotationID, char script[])
{
CCharacter_Script character;
character.ID_Name = nameID;
character.ID_Emotiation = emotationID;
character.str = script;

FILE* fp, * op;

fp = fopen("Save_Script.bin", "rb");

if (fp == NULL)
{
    fp = fopen("Save_Script.bin", "wb");
    fwrite(&character, sizeof(CCharacter_Script), 1, fp);
    fclose(fp);
}
else
{
    fclose(fp);

    op = fopen("Save_Script.bin", "ab");
    fwrite(&character, sizeof(CCharacter_Script), 1, op); // Use 'op' instead of 'fp'
    fclose(op);
}
}