K
K
Karmaev2021-11-08 00:43:29
C++ / C#
Karmaev, 2021-11-08 00:43:29

Why did .exe trigger a SI breakpoint when working with binary files?

I call these two functions: first I add the data to the file, then I read it into an array of structures and output it. The first function works, but when the second function is called, the "breakpoint is hit" when the fp variable is assigned a pointer to the stream - when the fopen() function is called. Please help me to solve the problem, I don't understand where is the error. Maybe it's because of global variables? The number_input function simply assigns a value from a pointer.

#include <stdio.h>

FILE* fp;
struct member* arr;
int arr_len = 0;

void add_data() {
    fp = fopen("database.bin", "wb");
    arr_len++;
    arr = realloc(arr, sizeof(struct member*) * arr_len);
    printf("  Input information about new member:");
    do {
        printf("\nFull name - ");
        getchar();
        gets(arr[arr_len - 1].full_name);
    } while (!contain_latin_letters(arr[arr_len - 1].full_name, strlen(arr[arr_len - 1].full_name)));
    do {
        printf("Club - ");
        gets(arr[arr_len - 1].club);
    } while (!contain_latin_letters(arr[arr_len - 1].club, strlen(arr[arr_len - 1].club)));
    do {
        printf("Role (goalkeeper, defender, midfielder, striker) - ");
        gets(arr[arr_len - 1].role);
    } while (!(!strcmp(arr[arr_len - 1].role, "goalkeeper") || !strcmp(arr[arr_len - 1].role, "defender") || !strcmp(arr[arr_len - 1].role, "midfielder") || !strcmp(arr[arr_len - 1].role, "striker")));
    number_input("Age of member - ", &arr[arr_len - 1].age);
    number_input("Number of matches played for team - ", &arr[arr_len - 1].matches);
    number_input("Number of scored goals - ", &arr[arr_len - 1].goals_scored);

    char* c = (char*)arr; // устанавливаем указатель на начало структуры
    // посимвольно записываем в файл структуру
    for (int i = 0; i < sizeof(struct member) * arr_len; i++)
    {
        putc(*c++, fp);
    }
    fclose(fp);
}

void output_database() {
    fp = fopen("database.bin", "rb");

    char* c = (char*)arr;
    // считываем посимвольно из файла
    int i;
    while ((i = getc(fp)) != EOF)
    {
        *c = i;
        c++;
    }
    for (i = 0; i < arr_len; i++)
        printf("%s   %s   %s   %d   %d   %d\n", arr[i].full_name, arr[i].club, arr[i].role, arr[i].age, arr[i].matches, arr[i].goals_scored);
    fclose(fp);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-11-08
@jcmvbkbc

The first function works, but when the second function is called, the "breakpoint is hit" when the fp variable is assigned a pointer to the stream - when the fopen() function is called. Please help me to solve the problem, I don't understand where is the error.

The second function is somehow very strongly tied to the first: it does not initialize arr and considers that there is enough space for the contents of the file. But this all happens after the stated problem.
In the first function, calls getsread lines of unlimited length into some fields of your structure. You didn't provide a definition for the structure, but my magic ball says that gets may well get out of the fields it reads into and break the heap. You can try replacing gets(x)with fgets(x, sizeof(x), stdin).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question