Online compiler runs my code, but VS don't

56 Views Asked by At

That's my code to write an table in a file. That's my homework, I had used methodological guidelines (?) from my University, I had seen a lot of StackOverflow pages with errors I found, but still can't fix this errors. In online compiler it works fine. I can show to teacher this work, but I want to know what's wrong. Hope you'll help with that. P.S.: file takes place in the same directory as program, so I didn't write full path to that. Error appears at the firts fprintf, so, that's the problem, guess

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


struct qinf { //structure of students
    char name[15];
    int year;
    int rate;
    float qual;
} qarr[10];

int main(void) {
    int n; 
    int i, j, m;
    float funk;
    struct qinf x;
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    for (n = 0; n < 10; n++) {
        printf("%d: Enter second name, year of birth, rate and qualification rate for work >> ", n + 1);
        scanf("%s", qarr[n].name);
        if (!strcmp(qarr[n].name, "***")) break;
        scanf_s("%d", &qarr[n].year);
        scanf_s("%d", &qarr[n].rate);
        scanf_s("%f", &funk); qarr[n].qual = funk;
    }
    
    FILE *fin;
    if (fin = fopen("test.txt", "w") != NULL) {

        fprintf(fin, "\n|-------------------------------------------------------------------------------------------|\n");
        fprintf(fin, "|                            Ведомость о присвоении квалификации                            |\n");
        fprintf(fin, "|-------------------------------------------------------------------------------------------|\n");
        fprintf(fin, "|       Фамилия      | Год рождения | Оценка рез-тов экзамена | Присвоен ур-нь квалификации |\n");
        fprintf(fin, "|-------------------------------------------------------------------------------------------|\n");

        for (i = 0; i < n; i++) {
            fprintf(fin, "| %18s | %12d | %23d | %27.2f |\n", qarr[i].name, qarr[i].year, qarr[i].rate, qarr[i].qual);
        }

        fprintf(fin, "|-------------------------------------------------------------------------------------------|\n");


        for (i = 0; i < n - 1; i++) {
            m = i;
            for (j = i + 1; j < n; j++) {
                if (strcmp(qarr[m].name, qarr[j].name) > 0) m = j;
                if (m > i) {
                    strcpy(x.name, qarr[i].name); x.year = qarr[i].year;
                    x.rate = qarr[i].rate; x.qual = qarr[i].qual;
                    strcpy(qarr[i].name, qarr[m].name); qarr[i].year = qarr[m].year;
                    qarr[i].rate = qarr[m].rate; qarr[i].qual = qarr[m].qual;
                    strcpy(qarr[m].name, x.name); qarr[m].year = x.year;
                    qarr[m].rate = x.rate; qarr[m].qual = x.qual;
                }

            }
        }

        fprintf(fin, "\n|-------------------------------------------------------------------------------------------|\n");
        fprintf(fin, "|                            Ведомость о присвоении квалификации                            |\n");
        fprintf(fin, "|-------------------------------------------------------------------------------------------|\n");
        fprintf(fin, "|       Фамилия      | Год рождения | Оценка рез-тов экзамена | Присвоен ур-нь квалификации |\n");
        fprintf(fin, "|-------------------------------------------------------------------------------------------|\n");

        for (i = 0; i < n; i++) {
            fprintf(fin, "| %18s | %12d | %23d | %27.2f |\n", qarr[i].name, qarr[i].year, qarr[i].rate, qarr[i].qual);
        }

        fprintf(fin, "|-------------------------------------------------------------------------------------------|\n");
        fclose(fin);
    }
    else printf("\n\nОшибка открытия файла");

    return 0;
}

Error:

an unhandled exception occurred at 0x00007FFDD833FAAD (ntdll.dll) 0xC0000005: 0xC0000005: Access violation writing location 0x0000000000000039

I tried to change path and place of file, change string inside frintf... That's all. Other actions I tried before I understood it's fprinf error, than I just don't know what to do else

0

There are 0 best solutions below