Problems in deleting all data from an employee

Asked

Viewed 57 times

3

After creating create and read I have been having problems in the update and delete code, but currently I have focused more on delete and I am not able to make it delete the entire employee.

Objective: the user inserts the employee ID and he erases all the data concerning the employee.

Unfortunately the result is far from being the request here is the code.

`

FILE *fRead, *fWrite;

char c;
int Delete_Id, temp = 1;

fRead = fopen(FILENAME, "r");
c = getc(fRead);

while (c != EOF) {
    printf("%c", c);
    c = getc(fRead);
}

rewind(fRead);

printf("\nDelete Staff with ID: ");
scanf("%d", &Delete_Id);

Delete_Id = Delete_Id + 1;

fWrite = fopen("temporary.tmp", "w");
c = getc(fRead);

while (c != EOF) {
    c = getc(fRead);
    if (c == '\n')
        temp++;
    if (temp != Delete_Id) {
        putc(c, fWrite);
    }
}

fclose(fRead);
fclose(fWrite);

remove(FILENAME);

rename("temporary.tmp", FILENAME);
printf("\nThe contents of file after being modified are as follows:\n");

fRead = fopen(FILENAME, "r");
c = getc(fRead);
while (c != EOF) {
    printf("%c", c);
    c = getc(fRead);
}
fclose(fRead)

The output resultinserir a descrição da imagem aqui

The output of the file:inserir a descrição da imagem aqui

From what I understood instead of deleting the data related to the function it just deletes according to the requested line and not his id, otherwise "eats" the first letters presented in txt and adds an ý at the end of the file.

staff variables

struct funcionario {
    
    int codigo1[100], idade[100], numero_telefone[100], numero_dependentes[100];
    char nome[100], estado_civil[100];

};

struct funcionario e;

Can someone help?? It’s been several days trying to solve this problem by checking several examples.

1 answer

-3

You want to use a file for each job?

If you are going to use it in table form, you should consider saving the information in a more conventional format like CSV, JSON or XML. For the sake of simplicity I would choose CSV.

To remove records from the list, I would not attempt to erase the data from that function, but rather, add a REMOVED attribute and change the state of that attribute only.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.