0
votes

I am coding in Visual Studio 2017 and I am making a vaccine inventory system. When adding the values to a txxt file however I am getting the error Debug Assertion Failed, I looked online and nothing worked. My code is below

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS

void main() {
    printf("Enter 1 for inventory creation \nEnter 2 for Updating Inventor \nEnter 3 for Vaccine Search \nEnter 4 for Distribution Details \n");
    int num1;
    scanf_s("%d", &num1);
    while (num1 != 1 && num1 != 2 && num1 != 3 && num1 != 4) {
        printf("Please enter a number between 1 and 4 \nEnter 1 for Inventory Creation \nEnter 2 for Updating Inventor \nEnter 3 for Vaccine Search \nEnter 4 for Distribution Details \n");
        num1 = 0;
        scanf_s("%d", &num1);
    }


if (num1 == 1) {
    char vaccineName[60];
    char vCode[2];
    char vCountry[60];
    int vDose;
    int popCovered;
    int vQuantity;
    printf("Enter Vaccine Name: \n");
    scanf_s("%s", vaccineName, _countof(vaccineName));
    printf("Enter Vaccine Code: \n");
    scanf_s("%s", vCode, _countof(vCode));
    printf("Enter Production Country: \n");
    scanf_s("%s", vCountry, _countof(vCountry));
    printf("Enter Vaccine Dosage: \n");
    scanf_s("%d", &vDose);
    printf("Enter Population Covered: \n");
    scanf_s("%d", &popCovered);
    printf("Enter Initial Quantity \n");
    scanf_s("%d", &vQuantity);

    FILE *fp;
    fp = fopen_s(&fp, "vaccine", "a");

    fprintf(fp, "%s", "\n \n");
    fprintf(fp, "Vaccine Name: %s \t", vaccineName);
    fprintf(fp, "Vaccine Code: %s \t", vCode);
    fprintf(fp, "Production Country: %s \t", vCountry);
    fprintf(fp, "Vaccine Dosage: %d \t", vDose);
    fprintf(fp, "Population Covered: %d \t", popCovered);
    fprintf(fp, "Quantity: %d \t", vQuantity);

    fclose(fp);
}
}

I have tried alternative I could find and I do not understand the error. The file name and location in my explorer is shown below

Location of my vaccine.txt file

Any help will be appreciated, thank you!

2
Please edit your question to contain minimal reproducible example as well as clear explanation what problem you have. "Nothing worked" is not very informative. Also choose what language you use - C and C++ are different languages. - Slava
You aren't checking the return value from open. - stark
I didn't think VS would let void main() through. It's wrong anyway. It should be int main(). Btw, I don't think C++ has scanf_s - Ted Lyngmo
Reminder: when allocating character arrays always consider the terminating nul character when declaring the capacity. - Thomas Matthews
fopen_s doesn't return a file pointer. It returns an error value. The file pointer is set because you pass a pointer to it to the function. Since 0 means no error you're overwriting a potentially valid file pointer with 0 or NULL. - Retired Ninja

2 Answers

0
votes

Your file to open is vaccine.txt, so you should try to open "vaccine.txt", not "vaccine".

0
votes

"Assertion failed" is nearly meaningless by itself. It is important to know which assertion failed. From the question title (but not its body) I gather that the failed assertion was stream != nullptr.

If that was caused by the code presented, then the source is almost certainly that the fopen() call failed. The return value in that case is a null pointer (and robust code tests for that). It appears that Microsoft's fprintf() implementation includes a debug assertion that the file (stream) pointer is not null, and that assertion fails, aborting the program.

Normally, attempting to open a file in append mode will succeed even if the file does not initially exist, creating the file if necessary, but there are good reasons why it might fail. For example, the file being inaccessible to the user running the program, or the file being absent and the program being unable to create it an account of (say) insufficient permissions on its working directory.