Register with C File

Asked

Viewed 665 times

2

How to know if there is a Record (struct) saved at any position in a C file ?

To the struct:

typedef struct Registro
{
     int chave;
     char caracteres[20];
     int idade;
     int prox;
     bool contem;
} Registro;

For example:

fseek(fl,0*sizeof(Registro),SEEK_SET);
fread(&registro,sizeof(Registro),1,fl);

Position 0 information was uploaded to registro. How to know if it exists ?

  • What is the structure of Registro?

  • typedef struct Record{ int key; char characters[20]; int age; int Prox; bool contains; }Record;

  • 1

    Edit your question to put this add-on, so it gets more organized.

  • Thanks for the tip!!

3 answers

2

What do you mean, "if he exists"????

If the result of fread() has been 1, the object registro has been filled in with information from stream; if the result was 0 there was an error that you can determine by errno

if (fread(&registro, sizeof (Registro), 1, fl) == 0) {
    perror("Registro");
    // exit(EXIT_FAILURE);
}
  • I need to check in the positions if there are already any records saved. So this way it works there ?

  • The fread() returns the number of items read, or 0 in case of error. If the contents of the file are not compatible with the structure, the fread() will not signal error, but the values obtained will be garbage

  • And how do I delete a record? Is there a function ?

  • There is no proper function to delete log. The usual is to write a new file, then delete the old one and rename the new one

2

In addition to the check that @pmg presented, I would recommend doing a check of the data read, to make sure that they are what you expect and make sure that the Registro exists.

For example, if you create a file arquivo.dat thus filled:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

And then read it like a Registro, your program will work perfectly. The file exists, has enough content to fill the sizeof(Registro) but the content is not of a Registro (That would be a false positive in your program):

caracteres: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa☻
chave: 1633771873
contem: 1633771873
idade: 1633771873
prox: 1633771873
  • But it will not be inserted this way in the file. Only via code itself, using fwrite(). So I can be unconcerned ?

  • It was just for example. Anyway, it does not prevent the user to edit the file and change it improperly or even to open the wrong file.

0

For example let’s say the file was created this way:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct Registro
{
 int chave;
 char caracteres[20];
 int idade;
 int prox;
 bool contem;
} Registro;

int main(){
Registro registro = {0,"",0,0,false};

FILE *fPtr;
if((fPtr = fopen("arquivo.dat","wb")) == NULL){
    printf("Erro na abertura do arquivo\n");
}
else{
     /* Inicializa dez campos "vazios" no arquivo,sem dados de usuario */
    for(i = 0 ; i < 10 ; i++){
         fwrite(&registro,sizeof(Registro),1,fPtr);
    }
    fclose(fPtr);
 }

return 0;
}

We can check if there are "empty" fields by doing the following:

int main(){

Registro registro = {0,"",0,0,false};

FILE *fPtr;
if((fPtr = fopen("arquivo.dat","rb")) == NULL){
    printf("Erro na abertura do arquivo\n");
}
else{
    fseek(fPtr,0*sizeof(Registro),SEEK_SET);
    fread(&registro,sizeof(Registro),1,fPtr);

    if(registro.chave == 0 && strcmp(registro.caracteres,"") == 0){
        printf("Esse campo esta vazio!\n");
    }
    else{
        printf("Esse campo contem dados!\n");
    }
    fclose(fPtr);

 }
return 0;
}

Browser other questions tagged

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