How can I solve run-time check failure error?

14 views Asked by At

I use visual studio 2022, this is part of the C code for Triple DES algorithm, and I have a question. The call of the Fileopen_text function is as follows(it's in main function.):

`input_text = Fileopen_text(&size, &remainder, &casenumber);`

These parameters are declared in the main function as follows: char remainder = 0; char casenumber = 0;

This is part of the codes of the fileopen_text function written in the process of entering the value into the casenumber variable are as follows :

`char* Fileopen_text(int* size, char* remainder, int* casenumber)
{
    int check_input = 0; 
    void* Ptr_input = 0; 
    int file_size = 0; 


    /* plaintext */
    FILE* Ptr_plain;
    fopen_s(&Ptr_plain, "plaintext.txt", "rb");
    if (Ptr_plain != NULL) check_input += 1;


    /* ciphertext */
    FILE* Ptr_cipher;
    fopen_s(&Ptr_cipher, "ciphertext.txt", "rb");
    if (Ptr_cipher != NULL) check_input += 2;


    switch (check_input)
    {
    case 1:
        Ptr_input = Ptr_plain;
        *casenumber = 1;
        break;
    case 2:
        Ptr_input = Ptr_cipher;
        *casenumber = 2;
        break;
    case 3:     // exist both plaintext and ciphertext
        printf("delete one file and run again.\n");
        fclose(Ptr_plain);
        fclose(Ptr_cipher);
        exit(1);
    default:    //not exist both plaintext and ciphertext
        printf("add a file and run again.\n");
        exit(1);
    }'

And this error occurs : Run-Time Check Failure #2 - Stack around the variable 'casenumber' was corrupted.

There are plaintext.txt in the same file with this c file. So, the value of casenumber is expected to be 1. Why is this nullptr? Any help would be greatly appreciated.

0

There are 0 answers