For example let's say that the file was created like this:
#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(®istro,sizeof(Registro),1,fPtr);
}
fclose(fPtr);
}
return 0;
}
We can check for "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(®istro,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;
}