0

I have an important project for uni in binary files in C. I have to put information in a relative binary file and it keeps showing me this "A breakpoint instruction (__debugbreak() statement or a similar call) was executed in tema.exe.", in the debugger.

What can I do?

This is the code

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

typedef struct {
    int id;
    int EsteSetat;
    char nume[100];
    int varsta;
    double salariu;
    char telefon[30];
}Angajat;


int articole(FILE* f, int lgart) // lgart este lungimea unui articol
{
    int pozp;
    int nrart;
    pozp = ftell(f); //memoreaza pozitia curenta a pointerului in fisier
    fseek(f, 0, 2);
    nrart = ftell(f) / lgart;
    fseek(f, pozp, 0);
    return nrart;
}

void scriere(FILE* f)
{
    int id;
    Angajat a;
    printf("Introduceti ID angajat: ");
    scanf("%d", &id);
    while (!feof(stdin))
    {
        if (id >= articole(f, sizeof(Angajat)))
        {
            a.EsteSetat = 0;
            fseek(f, 0, 2);
            for (int i = articole(f, sizeof(Angajat)); i <= id; i++)
            {
                fwrite(&a, sizeof(Angajat), 1, f);
            }
        }
        fseek(f, id * sizeof(Angajat), 0);
        fread(&a, sizeof(Angajat), 1, f);

        if (a.EsteSetat == 1)
            printf("Angajatul deja exista");
        else if (a.EsteSetat == 0)
        {
            a.EsteSetat = 1;
            a.id = id;
            printf("Introduceti numele angajatului: "); getchar(); gets(a.nume);
            printf("Varsta: "); scanf("%d", &a.varsta);
            printf("Salariu: "); scanf("%d", &a.salariu);
            printf("Telefon: "); scanf("%s", a.telefon);
            fseek(f, ftell(f) - sizeof(Angajat), 0);
            fwrite(&a, sizeof(Angajat), 1, f);
        }
        printf("Introduceti alt ID sau CTRL-Z+ENTER de 3 ori pt a iesi din fisier: "); scanf("%d", &id);
    }
}

int main()
{
    char numef[20];
    printf("Introduceti numele fisierului: ");
    scanf("%s", &numef);
    FILE* f = fopen(numef, "rb+");
    Angajat a;
    scriere(f);
    fclose(f);
    return 0;
}

thank you!

  • Please read [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Some programmer dude Apr 01 '23 at 10:27
  • You also do a lot of seeking back and forth which seems wrong. – Some programmer dude Apr 01 '23 at 10:28
  • And never ***ever*** use the `gets` function! It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that is has even been removed from the C language (over ten years ago). Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Apr 01 '23 at 10:30
  • 1
    This will happen when a function returns and the return address on the stack got corrupted. Which makes it jump to a random address, commonly it contains an INT3 instruction. Several places in this program where that could happen, the scanf() calls that read a string are dangerous. Using _CRT_SECURE_NO_WARNINGS was not a great idea. – Hans Passant Apr 01 '23 at 11:43

0 Answers0